getCurrentPosition crash

  • 1
  • Problem
  • Updated 5 years ago
  • Not a Problem
Hello, I am using this code to get gps coordinates every 3 sec. it works as i need.
but some times navigator.geolocation.getCurrentPosition stop working. I am using my app, everything is good, i close my app. 2-3 hours left, I open, my app again and can not get coordinates. Also open browser chrome or native browser and go http://www.w3schools.com/html/html5_g... to check coordinates, and it does not work too! it can not get my coordinates. only full reboot of phone helps me, and after that it works again very well.
I am using plugin.
What can be the reason geolocation crashes after my app ? the problem not very often, but sometimes happen.

function currentLocation(){
navigator.geolocation.getCurrentPosition(successLocation, errorLocation, {enableHighAccuracy: true, timeout: 10000, maximumAge: 0});
}

var successLocation = function(position){
localStorage.setItem('Lat', position.coords.latitude);
localStorage.setItem('Lon', position.coords.longitude);
};

var errorLocation = function(position){
console.log('errorLocation');
};

setInterval(currentLocation, 3000);
Photo of Mikhail Natarov

Mikhail Natarov

  • 14 Posts
  • 0 Reply Likes

Posted 5 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
getCurrentLocation in Android only works on significant location changes (usually more than 500 metres/half a mile). On smaller location changes, it will keep the first acquired location coordinates and return them, even though the user has moved, say, 100 m.
If you want consistent location changes to be visible, use watchPosition().

You may also want to read this
http://www.andygup.net/how-accurate-i...
Photo of LianjaDev

LianjaDev

  • 37 Posts
  • 3 Reply Likes
Looking at your code depending on how long it takes to acquire the geo location, you may be calling it again before it has completed.

Use a flag to ignore the timer expiry handler that calls getCurrentPosition() if it has not yet completed. Its possible the implementation code is not thread safe or reentrant.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Mikhail,

You may want to read this:
GPS is Unreliable