机器人的WebView地理位置 [英] android webview geolocation
问题描述
我要在的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
?
推荐答案
- 的JavaScript必须启用的
的WebView
,使用WebSettings.setJavaScriptEnabled(真);
- 在该应用程序需要的权限
ACCESS_FINE_LOCATION
-
在
的WebView
必须使用自定义WebChromeClient
它实现WebChromeClient.onGeolocationPermissionsShowPrompt ()
。这种方法 被称为由的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 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() );
这篇关于机器人的WebView地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!