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

后端 未结 2 1515
情话喂你
情话喂你 2021-02-05 14:57

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 = f         


        
相关标签:
2条回答
  • 2021-02-05 15:16

    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.

    0 讨论(0)
  • 2021-02-05 15:32

    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!

    0 讨论(0)
提交回复
热议问题