android webview 地理定位 [英] android webview geolocation

查看:47
本文介绍了android webview 地理定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 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, using WebSettings.setJavaScriptEnabled(true);
    • The app needs permission ACCESS_FINE_LOCATION
    • The WebView must use a custom WebChromeClient which implements WebChromeClient.onGeolocationPermissionsShowPrompt(). This method is called by the WebView 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 地理定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆