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

2023-09-12 23:49:08 作者:忆挽离笙歌

我一直使用PhoneGap的,它已经很大,但我碰到的一个问题,在Verizon的Droid的获取位置W / 2.0.1(按预期在G1 W / 1.6)。

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

如果我访问相同的网站在web视图,调用navigator.geolocation.getCurrentPosition不会产生JavaScript错误,但分享你的位置,不显示对话框,没有永远的回调被调用。在logcat中,我看到了什么似乎是一个相关的错误:10月2号至15号:37:00.413:ERROR / geolocationService(16871):抓安全例外注册从系统的位置更新这应该只发生在道姆prenderTree 。

有我认为web视图失败注册为位置更新,因为它不具备所要求的权限,而这又是不提示的权限的用户的结果。虽然有加入的Webkit包Android 2.0的相关GeoPermissions几种方法和对象,我一直无法使用其中的任何导致的WebView显示GeoPermission对话框。

Android安卓开发环境搭建详细教程

时,基于所述你好,从Android开发指南web视图示例以下,但它增加了一些在2.0中加入相关GeoPermissions该呼叫和对象。 *更新了适当的URL(从的作者 - 感谢奥利弗的!)。

有没有人之所以能得到这个工作?任何反馈将是巨大的,非常感谢!

 包com.example.android.helloactivity;

进口android.app.Activity;
进口android.os.Bundle;
进口android.webkit.GeolocationPermissions;
进口android.webkit.WebChromeClient;
进口android.webkit.WebView;
进口android.webkit.GeolocationPermissions.Callback;

公共类HelloActivity扩展活动实现GeolocationPermissions.Callback {

的WebView的WebView;
字符串geoWebsiteURL =htt​​p://maxheapsize.com/static/html5geolocationdemo.html;
公共HelloActivity(){
}

/ **
 *调用方式创建的第一个活动。
 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.hello_activity);

    的WebView =(web视图)findViewById(R.id.webview);
    webview.getSettings()setJavaScriptEnabled(真)。
    。webview.getSettings()setJavaScriptCanOpenWindowsAutomatically(真正的);
    webview.getSettings()setGeolocationEnabled(真)。 //好像如果我设置,web视图应及时当我打电话navigator.geolocation.getCurrentPosition
    GeolocationPermissions geoPerm =新GeolocationPermissions(); //添加API级别5,但没有方法暴露,直到API级别7
    GeoClient地缘=新GeoClient();
    webview.setWebChromeClient(GEO);
    字符串的起源=; //如何获得原产地在正确的格式?
    geo.onGeolocationPermissionsShowPrompt(原产地,这一点); //显然这不是怎样来使用,但预期使用没有记录
    webview.loadUrl(geoWebsiteURL);

}

公共无效的invoke(字符串出身,布尔允许,布尔记住){

}

最后一类GeoClient扩展WebChromeClient {

@覆盖
公共无效onGeolocationPermissionsShowPrompt(字符串产地,
回调回调){
// TODO自动生成方法存根
super.onGeolocationPermissionsShowPrompt(产地,回调);
callback.invoke(原产地,真,假);
}

}

}
 

解决方案

我只是想你code上的Nexus One采用Android 2.1,并能正常工作。请记住,你需要添加必要的权限,以你的清单:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
 

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).

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.

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."

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.

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!).

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

}

}

解决方案

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:

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