android webview 地理定位

android webview geolocation(android webview 地理定位)
本文介绍了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 地理定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)