Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.geolocation.getCurrentPosition Callbacks won't work on Firefox 10

I am building an app that uses the Geolocation API. I cant seem to get a very simple piece of code to work on Firefox 10. Here is the code:

    window.onload = function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                alert('it works');
            }, function(error) {
                alert('Error occurred. Error code: ' + error.code);         
            });
        }else{
            alert('no geolocation support');
        }
    };

So ,for example, in chrome, after running the page I will be asked if I want to share my location, and after clicking yes it will alert me with "it works". Now in Firefox 10 it will ask me to share my location and after clicking share it does nothing... I've been trying to get the callback to run any type of code but no luck. Is this a bug with Firefox or am I doing something wrong? I have an example of the code here for testing: http://dev-hub.com/geolocation.html.

Edit--- My OS is windows 7 64bit

like image 927
Zaptree Avatar asked Feb 09 '12 17:02

Zaptree


1 Answers

All right I found that the problem is indeed Firefox and that it does not work reliably or equally on all platforms. Looking at http://dev.w3.org/geo/api/spec-source.html I found the following option to add:

    window.onload = function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                alert('it works');
            }, function(error) {
                alert('Error occurred. Error code: ' + error.code);         
            },{timeout:5000});
        }else{
            alert('no geolocation support');
        }
    };

As you can see here the timeout:5000 has been added which means that if for some reason the browser takes more then 5000ms (5 seconds) then throw a timeout error (that's error code 3). So now whenever Firefox is not working it at least runs the error callback and i get an alert message of "Error occurred. Error code: 3".

Apparently the default value of timeout is infinite so it never times out... Chrome is 100% reliable but Firefox is about 10% reliable on my machine which is very disappointing. On my other computer which is running windows XP and is on the same network, Firefox seems to be 100% reliable.

like image 98
Zaptree Avatar answered Oct 16 '22 16:10

Zaptree