android webview 地理定位
我必须在 WebView
中检索用户的位置。我使用以下 Javascript 执行此操作:
function getLocation() {
navigator.geolocation.getCurrentPosition(displayLocation, handleError);
}
但权限请求弹出窗口永远不会打开。
我已设置以下设置:
ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
从 WebView
内访问用户位置的正确方法是什么?
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);
What is the correct way to access a user's location from within a WebView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
WebView
中启用 JavaScript,使用WebSettings.setJavaScriptEnabled(true);
ACCESS_FINE_LOCATION
WebView
必须使用实现WebChromeClient.onGeolocationPermissionsShowPrompt()
的自定义WebChromeClient
。这个方法由
WebView
调用以获得向 JavaScript 公开用户位置的权限。 (在浏览器的情况下,我们向用户显示提示。)默认实现不执行任何操作,因此永远不会获得权限,并且位置永远不会传递给 JavaScript。始终授予权限的简单实现是...地理定位使用数据库在会话之间保留缓存的位置和权限。数据库的位置是使用
WebSettings.setGeolocationDatabasePath(...)
设置的。如果未设置数据库的位置,持久存储将不可用,但地理定位将继续正常运行。要设置数据库的位置,请使用...WebView
, usingWebSettings.setJavaScriptEnabled(true);
ACCESS_FINE_LOCATION
The
WebView
must use a customWebChromeClient
which implementsWebChromeClient.onGeolocationPermissionsShowPrompt()
. This methodis 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 ...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 ...接受或拒绝用户位置的对话框是由程序员设计的:D。正如 Chris Cashwell 所说,你只需使用这样的回调即可:
在某些情况下,HTML5 需要使用存储,因此你必须启用一些属性,以便 webview 具有完全访问权限才能正常运行。
Dialog to accept or decline user location is design by programmer :D. As Chris Cashwell said, you just use a callback like this:
In some cases, HTML5 requires the use of storage, therefore you must enable some properties so that webview has full access to run normal.
分享我的工作活动类,这是一个完整的解决方案,可以演示
希望,它节省了某人的时间
Sharing my Working activity class, this is a complete solution which can demonstrate
Hope, it saves someone's time
您是否在清单中声明了该许可?
您可能还需要声明其他位置权限,例如:
Are you declaring that permission in your manifest?
You may also need to declare other location permissions, like these:
我最近遇到了这种情况,并采取了以下步骤来实现这一目标:
第 1 步:
在
AndroidManifest.xml
文件中添加权限第 2 步:创建一个包含
WebView
和ProgressBar
的活动(在我的例子中)步骤 3:在您的活动类中,添加以下代码并根据您的需要进行更改
I have recently come across this type of situation and have done below steps to achieve this:
Step 1:
Add permissions in your
AndroidManifest.xml
fileStep 2: Create an activity that would contain a
WebView
andProgressBar
(in my case)Step 3: In your activity class, add below code and make changes according to your need
这是显示警报对话框以提升用户使用其位置的权限的示例:
This is an example of showing alert dialog to promote for user permission to use his/her location:
当从 Webview 请求位置时,您需要动态请求权限
确保您已将 ACCESS_FINE_LOCATION 添加到清单
在顶级类中定义您的回调和来源,以便能够将它们分配给 ChromeClient 提供的回调和来源
处理地理位置请求和为回调分配值,以便在授予权限后能够使用它们
,一旦收到权限,就可以使用 origin 调用回调
you need to dynamically request for Permission when location is requested from Webview
Make sure You you have added ACCESS_FINE_LOCATION to Manifest
Define Your callback and origin at top level Class ,to be able to assign them to the callbacks and origin provided by ChromeClient
Handle geoLocation Request and Assign value to callbacks to be able to use them once permission is granted
once permission is received invoke calbacks using origin
作为更新的 Android 的新答案发布,所有内容都在一篇文章中,因为您不再需要使用
setWebChromeClient
。对于 Android 6+,您只需在运行时使用
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
请求 GPS 权限。Posting as a new answer for updated Android with everything in one post, because you no longer need to use
setWebChromeClient
.With android 6+ you just to ask for the GPS permissions at runtime using
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
.