我一直在使用PhoneGap,它很棒,但是我遇到了在带有2.0.1 的Verizon Droid 上获取位置的问题(在带有1.6 的G1 上按预期工作)。
GeoLocation API 支持在 2.0 (Eclair) 中添加到 Android,它可以在 Verizon Droid(2.0.1 上)的默认浏览器中运行。也就是说,如果我访问一个调用 navigator.geolocation.getCurrentPosition(success_callback, error_callback) 的网站,设备会在带有“共享位置”或“拒绝”选项的对话框中提示当前域“想知道你的位置”。如果我选择“共享位置”,success_callback 最终会被位置数据调用。
如果我在 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 中。”
在我看来,WebView 未能注册位置更新,因为它没有所需的权限,这反过来又是没有提示用户获得权限的结果。尽管在 Android 2.0 中的 Webkit 包中添加了几个与 GeoPermissions 相关的方法和对象,但我无法使用它们中的任何一个来使 WebView 显示 GeoPermission 对话框。
以下内容基于 Android 开发人员指南中的 Hello, WebView 示例,但它添加了一些在 2.0 中添加的与 GeoPermissions 相关的调用和对象。*使用适当的 url 更新(经作者许可 - 感谢 Oliver!)。
有没有人能够得到这个工作?任何反馈都会很棒,谢谢!
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);
}
}
}