Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap. Failed to start Geolocation service

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" />
like image 912
Robertas Setkus Avatar asked Oct 09 '12 09:10

Robertas Setkus


Video Answer


2 Answers

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});
};
like image 58
jmpenetra Avatar answered Sep 20 '22 21:09

jmpenetra


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

like image 26
Swayam Avatar answered Sep 18 '22 21:09

Swayam