PhoneGap的.无法启动地理定位服务

Rob*_*kus 6 android-emulator cordova

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);                
}
Run Code Online (Sandbox Code Playgroud)

总是在模拟器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" />
Run Code Online (Sandbox Code Playgroud)

jmp*_*tra 6

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

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

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

function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(myPositionSuccess, myPositionError, {maximumAge: 300000, timeout:10000, enableHighAccuracy : true});
};
Run Code Online (Sandbox Code Playgroud)


Swa*_*yam 6

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

navigator.geolocation.getCurrentPosition(onSuccess, onError,{enableHighAccuracy:true});
Run Code Online (Sandbox Code Playgroud)

而且,要指定模拟器中位置的坐标,

Eclipse - > Open Perspective - > DDMS - > Emulator Control - > Location Controls

指定位置的坐标,然后按"发送".