My HTML5 app uses Google Maps and tracks the user with navigator.geolocation.watchPosition, showing their location with a marker on the map.
I also have a toggle button. If the user clicks Off, I would like to stop tracking them with watchPosition.
This is my code so far:
$("#loc-label").click(function(){
if ($('#loc').is(':checked')) {
navigator.geolocation.clearWatch(watchId);
position_marker = null;
} else {
watchId = navigator.geolocation.watchPosition(geolocationWorked, geolocationFailed, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});
}
});
However, with this code, whenever the user toggles location on, they get re-promp开发者_运维知识库ted by the browser (in Firefox at least) asking them if they are willing to share their location.
Is there any way around this? Ideally I'd only like to prompt them once.
Obviously I could just hide the marker (but really continue to track their location) when the user toggles off. However, this isn't ideal, as I'm using high accuracy (i.e. GPS if available) and I'd prefer to give the user the ability genuinely to turn geolocation off.
Thanks!
I guess the behavior is device-specific. When testing the demo at http://www.thedotproduct.org/experiments/geo/ on my HTC Desire I get prompted only once unless I reload the page.
Of course the user won't be prompted again anyway if he chooses to permanently allow the page to access the users location (which I did not for the demo above).
Safari and Chrome (on mac at least) have settings to choose whether to ask. Haven't found that on Firefox yet; seems to prompt all the time.
精彩评论