I want to figure out what a client's position is.
Ideally they'd accept navigator.geolocation.getCurrentPosition but in the event that they won't, I'd like to make an ip lookup fallback and if that fails I'd like to just rely on timezone to roughly place the client's location.
I've made all of this, but navigator.geolocation.getCurrentPosition
doesn't trigger an error if the user just ignored the popup. The timeout
option seems to be how long it takes to resolve the location, not how long the request stays up for.
Is there a good way to auto-cancel the geo-tracking request pop-up after 10s and then fallback to something less accurate?
CodePudding user response:
Instead of using navigator.geolocation.getCurrentPosition
, use navigator.geolocation.watchPosition
for 10 seconds.
let watchId = navigator.geolocation.watchPosition(successFunction, errorFunction, {timeout: 10000});
When it times out, go back to another way of determining the location.
Docs: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition