3

The output geo coordinates are only accurate on first load, the problem is when the iPhone screen times out and the locations services also timeout. And if I open safari again the geo coordinates are off by 100's of meters. The same thing happens if I push safari app to background. The problem is fixed if I reload the safari tab requesting geolocation or if I clear safari app cache and open up safari again. Why is this happening? My use case involves showing the user an arrow that guides to destination. Thus it is likely the phone times out occasionally for user that leads to location services timeout. iOS chrome does not mimic this behavior and work as expected i.e geo coordinates are always accurate event after tab in focus and /or screen timeout.

jasan
  • 11,475
  • 22
  • 57
  • 97

1 Answers1

1

I don't know why this happens exactly, but it's happening to me too. I was able to get around the issue by using window.setInterval to call a function every 5 seconds or so that checks whether the location has been updated. If it hasn't, then the function stops and restarts the geolocation watch.

The restarting of the geolocation watch has the same effect as reloading the page, i.e. the accuracy becomes much better again, and location updates resume. Hope that helps!

Chadwick Wood
  • 2,012
  • 1
  • 15
  • 9
  • This worked for me. Thank you. Suggestion: add an event listener for the "focus" event (window.addEventListener('focus',func)) and restart your watchPosition in func. The best way to do this is to keep the return value from watchPosition so that when your focus listener fires you can use navigator.geolocation.clearWatch() to clear the previous watchPosition. Then you call navigator.geolocation.watchPosition again and save it's return value for next time. Check here for further info: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition – neveraskedforthis Jul 17 '20 at 20:01