在 Android 2.0+ 上的 WebView 中使用 navigator.geolocation.getCurrentPosition(PhoneGap 相关) [英] Using navigator.geolocation.getCurrentPosition in WebView on Android 2.0+ (PhoneGap related)

查看:38
本文介绍了在 Android 2.0+ 上的 WebView 中使用 navigator.geolocation.getCurrentPosition(PhoneGap 相关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 支持已在 2.0 (Eclair) 中添加到 Android 中,并且可在 Verizon Droid(2.0.1)的默认浏览器中运行.也就是说,如果我访问一个调用 navigator.geolocation.getCurrentPosition(success_callback, error_callback) 的网站,设备会在对话框中提示当前域想要知道您的位置",并带有共享位置"或拒绝"选项.如果我选择共享位置",最终会使用位置数据调用 success_callback.

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.

如果我在 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 中."

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 未能注册位置更新,因为它没有所需的权限,而这反过来又是未提示用户授予权限的结果.虽然在 Android 2.0 的 Webkit 包中添加了几个与 GeoPermissions 相关的方法和对象,但我一直无法使用它们中的任何一个来使 WebView 显示 GeoPermission 对话框.

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.

以下内容基于 Android 开发人员指南中的 Hello, WebView 示例,但它添加了一些在 2.0 中添加的与 GeoPermissions 相关的调用和对象.*使用适当的 url 更新(获得 的许可)作者 - 感谢奥利弗!)

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

}

}

推荐答案

我刚刚在装有 Android 2.1 的 Nexus One 上尝试了您的代码,并且运行良好.请记住,您需要向清单添加必要的权限:

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

这篇关于在 Android 2.0+ 上的 WebView 中使用 navigator.geolocation.getCurrentPosition(PhoneGap 相关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆