14

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zaptree
  • 3,763
  • 1
  • 31
  • 26
  • on what hardware are you running firefox? – tnt-rox Feb 09 '12 at 17:31
  • The Geolocation feature requires a mobile device that supports geolocation. [You can find out more here.](http://stackoverflow.com/questions/1349064/which-devices-support-javascript-geolocation-via-navigator-geolocation) – tnt-rox Feb 09 '12 at 17:53
  • So why do the other browsers correctly get my coords for where I live? And if that was the case why doesn't Firefox run the error callback with an error code 2 (position unavailable) or some other error like it should in such a case? – Zaptree Feb 09 '12 at 18:30
  • It uses your IP address or upstream IP or a wireless network – tnt-rox Feb 09 '12 at 18:50
  • I don't get your point? Obviously it does that but, the question is why doesn't Firefox do that when it is supposed to? I am convinced that there is a bug with either Firefox 10 or it just not working properly on my computer. For example http://maxheapsize.com/static/html5geolocationdemo.html a demo from http://stackoverflow.com/questions/2248404/about-geolocation-in-html-5 does not work on my Firefox browser when it should. I will test to see on my laptop and other PC and report my findings. – Zaptree Feb 09 '12 at 19:38
  • Well I solved the problem now but thanks for talking time to answer. – Zaptree Feb 09 '12 at 19:49
  • Sorry but the downvote is for a clearly wrong answer. – David Barker Aug 15 '12 at 14:02
  • Check this answer it works 100% on firefox.. http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt – Abhishek Asthana May 04 '13 at 17:28

2 Answers2

18

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.

Class
  • 3,149
  • 3
  • 22
  • 31
Zaptree
  • 3,763
  • 1
  • 31
  • 26
  • 7
    50000 ms=50 secs... so maybe should use 5000 ms (3 zeros) – michel.iamit Jun 03 '13 at 09:01
  • Additionally, Geolocation API Removed from Unsecured Origins in Chrome 50 https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en – QMaster Jul 02 '18 at 20:13
0

I've made this example for you:

if(!navigator.geolocation){
alert('El Navegador no soporta GeoLocalización');
}

function doGeo( position ) 
{
    var coords = position.coords.latitude + '+' + position.coords.longitude;
    var url = 'https://maps.google.es/?q=' + coords;
    $( "#lat" ).html("Latitud: " + position.coords.latitude );
    $( "#lon" ).html("Longitud: " + position.coords.longitude );
    $( "#acc" ).html("Precisión: " + position.coords.accuracy );
    $( "#alt" ).html("Altitud: " + position.coords.speed );        
    var link = '<a class="btn btn-primary" href="' + url + '" target="_blank">Ir a la     Ubicación en Google Maps</a>';
    $(link).appendTo('#GoogleMaps');
}

function lost()
{
    alert('Algo salió mal, Intentelo más tarde...');
};
navigator.geolocation.watchPosition(doGeo, lost, {maximumAge:0,enableHighAccuracy:true}          );

http://jsfiddle.net/aA2zv/35/

hope it helps!

user3307754
  • 21
  • 1
  • 4