I have a web app that asks the user for their GPS location. It works fine using pretty standard code that looks something like this:
function getpos_callback( a ) {
top.location.href = 'location.php?lat=' + a.coords.latitude + '&lon=' + a.coords.longitude;
}
function error_callback( er ) {
top.location.href = 'location.php?err=1';
}
if ( 'geolocation' in navigator ) {
navigator.geolocation.getCurrentPosition( getpos_callback, error_callback, {
enableHighAccuracy: true,
maximumAge: 300000, // 300 seconds = 5 min = max age of cached value to use
timeout: 8000, // 8 seconds
} );
}
Here's the problem: on a desktop browser, it can take quite some time (4-6 seconds, and as long as 8, per the code) to determine the location. Even with mobile sometimes it can be sluggish. These seconds feel like an eternity when the user just wants to get on with using the site.
What I'd like to do is let the user in right away, but somehow "spawn a task" to ask the browser for the location in the background, and then have it pass that location to me in the background when the location is retrieved.
Is this possible?