Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function fail never called if user declines to share geolocation in firefox

So I have this javascript code. In safari and chrome, if user declines to share location, it goes to fail function as it should; however, in Firefox, it does not.

Any helps appreciated.

function initGeolocation() {     if( navigator.geolocation )     {                     // Call getCurrentPosition with success and failure callbacks           navigator.geolocation.getCurrentPosition( success, fail );     }     else     {           alert("Sorry, your browser does not support geolocation services.");     } }   var map;  function success(position)  {       var longIDText = document.getElementById('longID');      var latIDText = document.getElementById('latID');      longIDText.value = position.coords.longitude;      latIDText.value = position.coords.latitude;      document.getElementById('coordSubmitID').click();   }    function fail(error)   {           alert("FAAAAAAAAAAIIIIIIIIIL")           var zip_code ;           while (true){               // Could not obtain location                zip_code = prompt("Please enter your current address or zip code","");                 if ( zip_code == "" ) {                   alert(zip_code +" is not a valid address. Please try again.");               }                else{                 break;               }           }           var zipIDText = document.getElementById('zipID');           zipIDText.value = zip_code;           document.getElementById('coordSubmitID').click();   } 
like image 693
theRealWorld Avatar asked May 10 '11 08:05

theRealWorld


People also ask

Why is geolocation not working?

If you have enabled the “Attempt to auto-locate the user” option on the settings page, and it doesn't work in Chrome, Safari or Firefox, then this is likely because of an insecure connection.

Which function is used to get the current position using geolocation API?

The Geolocation. getCurrentPosition() method is used to get the current position of the device.

What is geolocation error?

If you receive this error, it means your device supports geolocation but your mobile browser is unable to access it. To fix this, you need to have two things right: Your mobile device's location services (GPS) needs to be enabled. Your mobile browser needs to have an access to the geolocation provided by the phone.

What is geolocation watchPosition?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.


2 Answers

For Firefox it seems that PERMISSION_DENIED is raised only if "Never share" is selected; if the dialog is dismissed or "Not now" is selected, effectively nothing happens - even on mozillas geolocation demo if you dismiss the permissions UI nothing happens.

This means that getCurrentPosition can return either because the user closed the confirmation UI, or because it successfully started it asynchronous request - there doesn't appear to be a way to discriminate between the two.

https://bugzilla.mozilla.org/show_bug.cgi?id=675533

like image 167
Alex K. Avatar answered Sep 23 '22 10:09

Alex K.


This is a real pain, and definately not desirable functionality.

The workaround I am using to save the user waiting for ever is to set a timeout to check if the wait spinner is showing after 3 seconds, and if so, hide it and show a manual zip code input:

var latlng;  var waitTime = 3000; try {     if (navigator.geolocation) {         navigator.geolocation.getCurrentPosition(function (position) {             success(position);         }, showError);     } else {         showError("NOT-SUPPORTED");     }     var t = setTimeout(function () {         if ($("#getZip div.loading").css("display") != "none") {             $("#getZip div.loading").hide();             $("#errorZip").show();         }     }, waitTime); } catch (evt) {     alert(evt); } 
like image 25
Jamie Avatar answered Sep 25 '22 10:09

Jamie