Here's an approximation of my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
// success!
},
function(error) {
// error
},
{ timeout: 10000 }
);
} else {
// your browser/device doesn't support geolocation
}
When this code runs, the browser correctly asks the user for permission to track their physical location. If the user says "Yes", it correctly runs the function specified by the first argument ('success').
What's unclear to me is what happens when the user says "no". In my testing so far (in Firefox 7), if the user says "no", nothing happens. I was somewhat expecting the error callback (the second function) to run, but it doesn't. I'm hoping to react to the user's negative reponse (by removing the link that triggers this call).
You need to handle it in the second callback to navigator.geolocation.getCurrentPosition()
, which handles problems such as the user denying their location (amongs other things).
navigator.geolocation.getCurrentPosition(fn, function(errorCode) {
if (errorCode == 1) {
alert('Y U NO GIVE LOCATION?');
}
});
W3C Spec.
List of error codes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With