在网页视图上使用Android 2.0+ navigator.geolocation.getCurrentPosition(PhoneGap的相关)

StackOverflow https://stackoverflow.com/questions/2267513

我一直与PhoneGap的,它是伟大的,但我已经与Verizon的Droid的获取位置W / 2.0.1(如预期在G1 W / 1.6作品)遇到了问题。

地理位置API支持加入到Android在2.0(埃克莱尔)和它的工作原理在默认浏览器上的Verizon的德罗伊德(上2.0.1)。也就是说,如果我访问一个网站,呼吁navigator.geolocation.getCurrentPosition(success_callback,error_callback),设备将提示当前域“想知道你的位置”的选项中一个对话框,以“共享位置”或“下降”。如果我选择“共享位置”,success_callback最终被调用的位置数据。

如果我访问同一个网站的网页视图,调用navigator.geolocation.getCurrentPosition不会产生JavaScript错误,但“分享你的位置”未显示对话框,既不回调曾经被调用。在logcat中,我看到了什么似乎是一个相关的错误:“10月2日至15日:37:00.413:ERROR / geolocationService(16871):抓安全例外登记从系统这应该只在DumpRenderTree发生位置更新”

在我看来,该Webview是无法注册的位置更新,因为它不具备所需的权限,而这又是不是提示输入权限的用户的结果。虽然有添加到包的Webkit在Android 2.0的有关GeoPermissions几种方法和目的,我还没有能够使用它们中的任何导致web视图以显示GeoPermission对话框。

是基于你好,从Android开发者指南的WebView示例以下,但它增加了一些在2.0中添加的有关GeoPermissions调用和对象。 *更新了适当的URL(经许可从笔者 - !感谢奥利弗的)

有没有人能得到这个工作?任何反馈将是巨大的,谢谢!

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);
}

}

}
有帮助吗?

解决方案

我只是想你的代码运行在Nexus One的Android 2.1系统,并能正常工作。请记住,你需要必要的权限添加到您的清单:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

其他提示

我们(的PhoneGap的团队)最近发布了一个补丁这一点。基本上,问题是,由于Android 2.0,原生的WebView现在有它自己的实现navigator.geolocation的,因而PhoneGap的桥梁,为这个功能未能恰当在JavaScript设置。

此后,特定提交创造了一种解决方法:我们“代理”本地navigator.geolocation对象我们自己的这个对象的定义。该目的用的PhoneGap框架效果很好。

有关此修复,请参阅下列提交: http://github.com/的PhoneGap / PhoneGap的-机器人/提交/ 5255f632377c36beb0b4d2620940f33b6d02b2c7

此链接有钥匙 http://cordova.apache.org/docs/en/ 2.5.0 / cordova_device_device.md.html

我与Android显影 - PhoneGap的

我添加

应用程序/ RES / XML / config.xml中

<plugin name="Device" value="org.apache.cordova.Device" />

应用程序/ AndroidManifest.xml中

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top