在Android 2.0+上使用WebView中的navigator.geolocation.getCurrentPosition(与PhoneGap相关)

ajh*_*158 13 android geolocation webview cordova

我一直在使用PhoneGap并且它很棒,但是我遇到了在Verizon Droid w/2.0.1上获得位置的问题(在G1 w/1.6上按预期工作).

GeoLocation API支持已添加到Android 2.0(Eclair)中,它可以在Verizon Droid的默认浏览器中使用(在2.0.1上).也就是说,如果我访问一个网站,呼吁navigator.geolocation.getCurrentPosition(success_callback,error_callback),设备将提示当前域"想知道你的位置"的选项中一个对话框,以"共享位置"或"下降".如果我选择"共享位置",则最终会使用位置数据调用success_callback.

如果我在WebView中访问同一个网站,则对navigator.geolocation.getCurrentPosition的调用不会生成javascript错误,但不会显示"共享您的位置"对话框,也不会调用任何回调.在logcat中,我看到了什么似乎是一个相关的错误:"10月2日至15日:37:00.413:ERROR/geolocationService(16871):抓安全例外登记从系统这应该只在DumpRenderTree发生位置更新"

在我看来,WebView无法注册位置更新,因为它没有所需的权限,而这又是不提示用户获得权限的结果.尽管在Android 2.0中与GeoPermissions相关的Webkit包中添加了几个方法和对象,但我无法使用它们中的任何一个来使WebView显示GeoPermission对话框.

以下内容基于Android Developer's Guide中的Hello,WebView示例,但它添加了2.0中与GeoPermissions相关的一些调用和对象.*使用适当的网址更新(获得作者许可- 感谢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);
}

}

}
Run Code Online (Sandbox Code Playgroud)

Rom*_*rik 23

我刚刚在带有Android 2.1的Nexus One上尝试了你的代码,它运行正常.请记住,您需要为清单添加必要的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)