【发布时间】:2013-10-07 20:39:42
【问题描述】:
我有一个 WebView 可以拉出一个使用 Google Maps API 的网页,并且我有一个名为 MapsBase 的对象来处理地图内容。在 MapsBase init() 我有:
window.addEventListener('blur', (function (obj) {
return function (evt) { obj.Pause(); };
})(this), true);
window.addEventListener('focus', (function (obj) {
return function (evt) { obj.Resume();};
})(this), true);
作用于这些函数:
MapsBase.prototype.Pause = function () {
//Chrome bug - focus and blur events are called twice.
if (this.isPaused) {
return;
}
if (window['DEBUG']){window.console.log('MapsBase.prototype.Pause()');}
this.isPaused = true;
this.geolocationMarker.setMap(null);
};
MapsBase.prototype.Resume = function () {
if (this.isPaused) { //Chrome bug - focus and blur events are called twice.
if (window['DEBUG']){window.console.log('MapsBase.prototype.Resume()');}
this.isPaused = false;
this.geolocationMarker.setMap(this.map);
}
};
在 Android 端,我有我的 WebView(扩展 Fragment),它在 onStart() 中使用它:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setGeolocationEnabled(true);
settings.setGeolocationDatabasePath(context.getFilesDir().getPath());
并将其作为 onPause 方法:
@Override
public void onPause(){
if (MainActivity.DEV_MODE) { Log.i("WebFragment","onPause - dispatching the 'blur' event"); }
webView.getSettings().setGeolocationEnabled(false);
super.onPause();
}
似乎所有事件都按计划触发。当我在 Nexus 4 上监控调试输出时,我看到 onPause 方法正在触发并调度“模糊”事件,但地理位置仍然处于活动状态(显示在通知区域中)。
tl;博士:
有没有办法强制 WebView 放弃地理位置?
编辑:
使用后退按钮(系统)退出 web 视图时,地理定位似乎被停用,但当我使用 Navigation Drawer 导航离开时,就像我要打开抽屉并转到应用程序的完全不同的部分。如果我只是按主页按钮(系统“主页”返回启动器)也会发生这种情况。
当我使用导航抽屉导航到应用程序的不同部分时,以下是日志输出(在此之后,地理位置仍然处于活动状态):
编辑 2:
关于开启/关闭 GPS 的问题有很多。
特别是,一个似乎相关的问题是Switch off GPS programatically。选择的问题和答案似乎都符合我的要求,但似乎有点矫枉过正。我没有LocationListener,为了得到一个,我需要遵循Location Strategies 指南。
似乎通过WebView 将地理位置设置为禁用应该可以解决问题(就像我在onPause 中所做的那样),但事实并非如此。
我在 SO 上看到的另一个选项是完全禁用应用程序的 GPS,isn't possible 除非通过利用已修复的安全漏洞。
这是WebView 代码中的错误吗?
编辑 3:
我找到了解决当前问题的方法,这是我的 JavaScript 中的一个错误。地理定位标记的 setMap 函数未清除为地图设置的回调函数。
至于更大的问题,为什么网页Geolocation会否决WebView的地理位置设置?原来这个this was a bug已经被标记为“fixed”(在至少对于Activity)。我怀疑这是一种回归,当WebView 在Fragment 中运行时会出现。
【问题讨论】:
-
你找到解决办法了吗?
-
@mohitum007 根本原因(错误)仍未修复,两天前我刚刚再次检查。我的直接解决方案(解决方法)来自确保从网页中清除所有事件侦听器和回调函数。
标签: javascript android google-maps-api-3 webview geolocation