I am facing an issue with stucked gps coordinates using geolocation in a pwa. The problem is that sometimes, the coordinates stucked in the previous location. For example I got the location of a place, the user was an hour ago.
I believe this cases I got the location of the last location the gps could track.
Is there anyway to refresh the gps, and only send the data once gps has valid, refreshed connection?
The code for geolocation:
if ("geolocation" in navigator && navigator.geolocation != null) {
try {
navigator.geolocation.getCurrentPosition(position => {
//if geolocation has valid connection
console.log(position);
$('#geolocation_form_latitude').val(position.coords.latitude);
$('#geolocation_form_longitude').val(position.coords.longitude);
$('#geolocation_form').submit();
}, error => {
$('#geolocation_form').submit();
})
}
catch (e) {
$('#geolocation_form').submit();
}
}
Mobile OSes routinely do this kind of cacheing for battery management - it reduces the need to power up energy-intensive radios such as GPS and WiFi. Hence most mobile browsers will also demonstrate this stale location behaviour because they are dependent on the OS's location services.
Attempt to use the options in the HTML5 geolocation API to see if it helps get a better fix. Also note that both Safari and Chrome on iOS return additional properties on the returned geolocation object as suggested in this Chrome developer documentation - I notice a key called "Cache Age" when loading this test page which may be used to verify freshness of the fix.
maximumAge Is a positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age. Default: 0.
enableHighAccuracy Is a boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.