Simone Sessa
Simone Sessa

Reputation: 873

navigator.geolocation.getCurrentPosition with Cordova

I've a strange problem with the Cordova's geolocation plugin.
Works perfecly with my Tablet (ASUS TF201, Android 4.4.2), but it doesn't works with my phone (LG L9, Android 4.1.2) and with emulator (Nexus 5, Android 4.4.4).
It prints nothing, not even the onErrorGeo function..
This is the code:

var onSuccessGeo = function(position) {  
  $("#geolocalization").html('Latitude: '          + position.coords.latitude          + '<br>' +
      'Longitude: '         + position.coords.longitude         + '<br>' +
      'Altitude: '          + position.coords.altitude          + '<br>' +
      'Accuracy: '          + position.coords.accuracy          + '<br>' +
      'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '<br>' +
      'Heading: '           + position.coords.heading           + '<br>' +
      'Speed: '             + position.coords.speed             + '<br>' +
      'Timestamp: '         + position.timestamp                + '<br>');
};
function onErrorGeo(error) {  
  $("#geolocalization").html('code: '    + error.code    + '<br>' +
      'message: ' + error.message + '<br>');
}
function Geolocal(){
  navigator.geolocation.getCurrentPosition(onSuccessGeo, onErrorGeo);
}

Upvotes: 2

Views: 5500

Answers (2)

superjisan
superjisan

Reputation: 2074

So I also came upon this issue, but I figured it out according to Mozilla's documentation, which the cordova plugin follows here: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation

In the documentation it states that you can supply a PositionOption as the third parameter for navigator.geolocation.getCurrentPosition.See here for documentation: https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions

So if you change your code to

function onDeviceReady() 
   navigator.geolocation.getCurrentPosition(onSuccessGeo, onErrorGeo, {timeout: 5000});
}

It will timeout error after 5 seconds, otherwise the default timeout is Infinity, so it will continue to wait until the code is fired, which might be never if you have your location turned off or GPS is unable to get the proper location.

Hope this helps.

Upvotes: 3

HischT
HischT

Reputation: 953

The code looks correct to me, but just to be sure you can try using alert messages like this

function onErrorGeo(error) {  
   alert("error");
}

to make sure the error function is really not working. You can do this in the success function too.

In the case your code is not working, make sure you are executing

navigator.geolocation.getCurrentPosition(onSuccessGeo, onErrorGeo);

after the PhoneGap event deviceready has been launched.

You can try this:

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

function onDeviceReady() 
   navigator.geolocation.getCurrentPosition(onSuccessGeo, onErrorGeo);
}

Update: You should also make sure the config.xml of your application has this declared somewhere within the widget tag:

<gap:plugin name="org.apache.cordova.geolocation" />

Upvotes: 0

Related Questions