Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.geolocation.getCurrentPosition(): fire callback if user says "no"?

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).

like image 962
brianjcohen Avatar asked Nov 17 '11 23:11

brianjcohen


1 Answers

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.

like image 144
alex Avatar answered Nov 03 '22 04:11

alex