在 Android 2.0+ 上的 WebView 中使用 navigator.geolocation.getCurrentPosition(PhoneGap 相关)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2267513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using navigator.geolocation.getCurrentPosition in WebView on Android 2.0+ (PhoneGap related)
提问by ajh158
I've been working with PhoneGap and it's been great, but I've run into a problem with getting location on a Verizon Droid w/ 2.0.1 (works as expected on a G1 w/ 1.6).
我一直在使用 PhoneGap 并且它很棒,但是我遇到了在 Verizon Droid w/2.0.1 上获取位置的问题(在 G1 w/1.6 上按预期工作)。
GeoLocation API Support was added to Android in 2.0 (Eclair) and it works in the default browser on a Verizon Droid (on 2.0.1). That is, if I visit a website that calls navigator.geolocation.getCurrentPosition(success_callback, error_callback), the device prompts that the current domain "wants to know your location" in a dialog with options to "Share location" or "decline". If I select "Share location", success_callback eventually gets called with location data.
GeoLocation API 支持是在 2.0 (Eclair) 中添加到 Android 中的,它可以在 Verizon Droid(2.0.1)上的默认浏览器中运行。也就是说,如果我访问一个调用 navigator.geolocation.getCurrentPosition(success_callback, error_callback) 的网站,设备会在对话框中提示当前域“想要知道您的位置”,并带有“共享位置”或“拒绝”选项。如果我选择“共享位置”,success_callback 最终会被调用位置数据。
If I visit the same website in a WebView, the call to navigator.geolocation.getCurrentPosition does not generate a javascript error, but the "share your location" dialog is not displayed and neither callback ever gets called. In logcat, I see what seems to be a related error: "02-15 10:37:00.413: ERROR/geolocationService(16871): Caught security exception registering for location updates from system. This should only happen in DumpRenderTree."
如果我在 WebView 中访问同一个网站,对 navigator.geolocation.getCurrentPosition 的调用不会生成 javascript 错误,但不会显示“共享您的位置”对话框,也不会调用任何回调。在 logcat 中,我看到了一个相关的错误:“02-15 10:37:00.413: ERROR/geolocationService(16871): Caught security exception registering for location updates from system。这应该只发生在 DumpRenderTree 中。”
It seems to me that the WebView is failing to register for location updates because it does not have the required permission, which in turn is a result of not prompting the user for the permission. Although there were several methods and objects added to the Webkit package in Android 2.0 related to GeoPermissions, I haven't been able to use any of them to cause WebView to display the GeoPermission dialog.
在我看来,WebView 未能注册位置更新,因为它没有所需的权限,而这又是由于未提示用户授予权限的结果。虽然在 Android 2.0 的 Webkit 包中添加了几个与 GeoPermissions 相关的方法和对象,但我一直无法使用它们中的任何一个来使 WebView 显示 GeoPermission 对话框。
The following is based on the Hello, WebView example from the Android Developer's Guide but it adds some of the calls and objects that were added in 2.0 related to GeoPermissions. *Updated with an appropriate url (with permission from the author - thanks Oliver!).
以下内容基于 Android 开发人员指南中的 Hello, WebView 示例,但它添加了一些在 2.0 中添加的与 GeoPermissions 相关的调用和对象。*使用适当的网址更新(经作者许可-感谢奥利弗!)。
Has anyone been able to get this working? Any feedback would be great, thanks!
有没有人能够让这个工作?任何反馈都会很棒,谢谢!
package com.example.android.helloactivity;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.GeolocationPermissions.Callback;
public class HelloActivity extends Activity implements GeolocationPermissions.Callback{
WebView webview;
String geoWebsiteURL = "http://maxheapsize.com/static/html5geolocationdemo.html";
public HelloActivity() {
}
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_activity);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setGeolocationEnabled(true); //seems like if i set this, the webview should prompt when I call navigator.geolocation.getCurrentPosition
GeolocationPermissions geoPerm = new GeolocationPermissions(); //added in API Level 5 but no methods exposed until API level 7
GeoClient geo = new GeoClient();
webview.setWebChromeClient(geo);
String origin = ""; //how to get origin in correct format?
geo.onGeolocationPermissionsShowPrompt(origin, this); //obviously not how this is meant to be used but expected usage not documented
webview.loadUrl(geoWebsiteURL);
}
public void invoke(String origin, boolean allow, boolean remember) {
}
final class GeoClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback) {
// TODO Auto-generated method stub
super.onGeolocationPermissionsShowPrompt(origin, callback);
callback.invoke(origin, true, false);
}
}
}
回答by Roman Nurik
I just tried your code on a Nexus One with Android 2.1, and it works fine. Remember that you'll need to add the necessary permissions to your manifest:
我刚刚在装有 Android 2.1 的 Nexus One 上尝试了您的代码,并且运行良好。请记住,您需要向清单添加必要的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
回答by fil maj
We (the PhoneGap team) have recently released a fix for this. Basically, the problem was that as of Android 2.0, the native WebView now has its own implementation of navigator.geolocation, and thus the PhoneGap bridge for this function was not getting set properly in JavaScript.
我们(PhoneGap 团队)最近为此发布了一个修复程序。基本上,问题在于从 Android 2.0 开始,本机 WebView 现在有自己的 navigator.geolocation 实现,因此此功能的 PhoneGap 桥没有在 JavaScript 中正确设置。
Since then, a particular commit has created a workaround for this: we 'proxy' the native navigator.geolocation object to our own definition of this object. This object works well with the PhoneGap framework.
从那时起,一个特定的提交为此创建了一个解决方法:我们将本机 navigator.geolocation 对象“代理”到我们自己定义的这个对象中。此对象适用于 PhoneGap 框架。
For this fix, see the following commit: http://github.com/phonegap/phonegap-android/commit/5255f632377c36beb0b4d2620940f33b6d02b2c7
对于此修复,请参阅以下提交:http: //github.com/phonegap/phonegap-android/commit/5255f632377c36beb0b4d2620940f33b6d02b2c7
回答by joseAndresGomezTovar
This link has the key http://cordova.apache.org/docs/en/2.5.0/cordova_device_device.md.html
此链接具有密钥 http://cordova.apache.org/docs/en/2.5.0/cordova_device_device.md.html
I am developing with Android - Phonegap
我正在使用 Android 开发 - Phonegap
I added
我加了
app/res/xml/config.xml
应用程序/res/xml/config.xml
<plugin name="Device" value="org.apache.cordova.Device" />
app/AndroidManifest.xml
应用程序/AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />