问题
PhoneGap version: 2.0.0. Android API level 16 (4.0.3 version). Code sample which prompts an error:
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);
}
Always getting an error in emulator Failed to start Geolocation service
, error code 2
. Even if send GPS coordinates through Android console or DDMS
Android manifest permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
回答1:
Are you testing this on the emulator only? The Geolocation Service always fails in my Android emulator but not on a real device.
EDIT: Please try using the following code
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(myPositionSuccess, myPositionError, {maximumAge: 300000, timeout:10000, enableHighAccuracy : true});
};
回答2:
You need to set the enableHighAccuracy
option to true
while you are trying to get the location.
navigator.geolocation.getCurrentPosition(onSuccess, onError,{enableHighAccuracy:true});
Moreover, to specify the coordinates of your position in the emulator,
Eclipse -> Open Perspective -> DDMS -> Emulator Control -> Location Controls
Specify the coordinates of the position and press "Send" .
回答3:
I have try to fix this issue for weeks. I search the net and it seems that most people is having this issue regarding phonegap whenever they are developing for android. The solution was to test my code on a device that has GPRS. Also make sure that before runnning your code your GPRS and wifi is enable. Avoid using emulator to test GPRS because there are lots of features missing and bugs.
来源:https://stackoverflow.com/questions/12797172/phonegap-failed-to-start-geolocation-service