问题描述
我必须在 WebView
中检索用户的位置.我使用以下 Javascript 执行此操作:
I have to retrieve a user's location in a WebView
. I do this with the following Javascript:
function getLocation() {
navigator.geolocation.getCurrentPosition(displayLocation, handleError);
}
但是权限请求弹出窗口永远不会打开.
But the permission request popup never opens.
我已经设置了这些设置:
I've set these settings:
ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
从 WebView
中访问用户位置的正确方法是什么?
What is the correct way to access a user's location from within a WebView
?
推荐答案
- 必须在
WebView
中启用 JavaScript,使用WebSettings.setJavaScriptEnabled(true);
- 应用需要权限
ACCESS_FINE_LOCATION
WebView
必须使用实现WebChromeClient.onGeolocationPermissionsShowPrompt()
的自定义WebChromeClient
.这种方法由WebView
调用以获取向 JavaScript 公开用户位置的权限.(在浏览器的情况下,我们向用户显示一个提示.)默认实现什么都不做,所以永远不会获得许可,也永远不会将位置传递给 JavaScript.始终授予权限的简单实现是...- JavaScript must be enabled in the
WebView
, usingWebSettings.setJavaScriptEnabled(true);
- The app needs permission
ACCESS_FINE_LOCATION
The
WebView
must use a customWebChromeClient
which implementsWebChromeClient.onGeolocationPermissionsShowPrompt()
. This method is called by theWebView
to obtain permission to disclose the user's location to JavaScript. (In the case of the browser, we show a prompt to the user.) The default implementation does nothing, so permission is never obtained and the location is never passed to JavaScript. A simple implementation which always grants permission is ...webView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } });
地理定位使用数据库在会话之间保持缓存的位置和权限.使用
WebSettings.setGeolocationDatabasePath(...)
设置数据库的位置.如果未设置数据库的位置,则持久存储将不可用,但 Geolocation 将继续正常运行,否则.要设置数据库的位置,请使用 ...Geolocation uses databases to persist cached positions and permissions between sessions. The location of the database is set using
WebSettings.setGeolocationDatabasePath(...)
. If the location of the database is not set, the persistent storage will not be available, but Geolocation will continue to function correctly otherwise. To set the location of the databases, use ...webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );
这篇关于android webview 地理定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!- JavaScript must be enabled in the