I have a Chrome extension which uses the W3C Geolocation API to get the user's location. The extension is mostly used on Chromebooks.
I have noticed that the API seems to return the same coordinates (cached ?) most of the time, even though I have the maximumAge parameter set to 0. I tested this by deploying the extension on a Chromebook at location #1, then took the device to location #2, which is about 1 kilometer away. There the API was called again and it returned the same coordinates as before. A while later (I had it on a 10 minute interval) it returned the correct coordinates, but when I came back to location #1, I got cached coordinates again, ths time from location #2.
I tried with getCurrentPosition
, watchPosition
and several combinations of the options object, yet it always seems to produce the same outcome.
My current code is as follows:
function getGeolocation() {
return new Promise((resolve, reject) => {
const options = { enableHighAccuracy: true, timeout: 30000, maximumAge: 0 };
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
}
getGeolocation()
.then(position => handler(position))
.catch(err => console.error(err));
I need this to return the correct coordinates on first try or within a reasonable short time frame. Any advice?
Thanks!
EDIT:
To note that changing coordinates in the browser's sensors tabs works perfectly every time, but in a real situation it just seems to return cached results in most cases.
EDIT (Nov 5th):
After some more testing it seems that this occurs only on Chrome OS. We could not reproduce the same behavior on a PC (with or without an extension).