6

PhoneGap 版本:2.0.0。Android API 级别 16(4.0.3 版本)。提示错误的代码示例:

navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true});

function onSuccess(position) {
    console.log('latitude: '+ position.coords.latitude);
    console.log('longitude: '+ position.coords.longitude);                  
}

function onError(error) {
    console.log('Appeared error : '+ error.message);                
}

总是在模拟器Failed to start Geolocation service中出错,错误代码2。即使通过Android 控制台DDMS发送 GPS 坐标

Android清单权限:

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

3 回答 3

6

您需要在尝试获取位置时将enableHighAccuracy选项设置为。true

navigator.geolocation.getCurrentPosition(onSuccess, onError,{enableHighAccuracy:true});

此外,要指定您在模拟器中的位置坐标,

Eclipse -> 打开透视图 -> DDMS -> 模拟器控制 -> 位置控制

指定位置的坐标,然后按“发送”

于 2012-12-08T09:57:57.390 回答
6

您是否仅在模拟器上进行测试?地理位置服务在我的 Android 模拟器中总是失败,但在真实设备上却没有。

编辑:请尝试使用以下代码

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(myPositionSuccess, myPositionError, {maximumAge: 300000, timeout:10000, enableHighAccuracy : true});
};
于 2012-10-23T22:25:32.943 回答
0

我已经尝试解决这个问题好几个星期了。我在网上搜索,似乎大多数人在为 android 开发时都会遇到关于 phonegap 的问题。解决方案是在具有 GPRS 的设备上测试我的代码。还要确保在运行代码之前启用您的 GPRS 和 wifi。避免使用模拟器测试GPRS,因为有很多功能缺失和错误。

于 2014-09-25T19:11:10.383 回答