getCurrentPosition() not working in firefox

2019-07-22 05:19发布

I'm trying to geolocate with the navigator.geolocation.getCurrentPosition() function, but I can't seem to get it working on firefox. More specifically, I made a very simple HTML file:

<!DOCTYPE HTML>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
<script type="text/javascript" src="js/mapHandler.js"></script>
</head>
<body>
</body>
</html>

As you can see, it's the bare minimum of an HTML file. The javascript just does the following:

function displayLocation (position)
{
alert("displayLocation");
}
function displayError(positionErr)
{
alert("error");
}
function initialize()
{
 if (!(navigator.geolocation == 'undefined'))
  {
   navigator.geolocation.getCurrentPosition(displayLocation, displayError,{timeout:10000});
  }
 else
  {
   alert('Geolocation unsupported');
  }
} 
$(document).ready(function(){initialize() });

Once again, it's a very very simple example which just asks for the position and makes a couple of alert. The strange thing is that when I launch the script I can't seem to get any answer. Does anyone know why? Thanks in advance :)

3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-22 05:34

Seem's the note is important :

http://www.w3schools.com/html/html5_geolocation.asp

Browser Support

Internet Explorer 9+, Firefox, Chrome, Safari and Opera support Geolocation.

Note: Geolocation is much more accurate for devices with GPS, like iPhone.

Done the try it test : http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

-> On computer using Firefox 26.0 : Nothing happened. Even when sharing position (No GPS device connected).

-> On android smartphone using firefox 27.0 : Get Latitude & Longitude from GPS

查看更多
Bombasti
3楼-- · 2019-07-22 05:38

Actually I tested it on Chrome 32(It worked fine) and Firefox 26 (No luck).

I tried enabling geolocation on Firefox but still doesn't work.

I have browsed couple sites on the google and finally What I understood is Firefox 26 & 27 has a bug in detecting the geolocation.

For testing the functionality, you can run this LIVE DEMO which is a plain javascript.

initialize();

function displayLocation(position) {
    var cords = position.coords;
    alert("displayLocation, lat='"+cords.latitude+"'; long='"+cords.longitude+"'");
}

function displayError(positionErr) {
    alert("error");
}

function initialize() {
    if (!(navigator.geolocation == 'undefined')) {
        navigator.geolocation.getCurrentPosition(displayLocation, displayError, {
            timeout: 10000
        });
    } else {
        alert('Geolocation unsupported');
    }
}
查看更多
Bombasti
4楼-- · 2019-07-22 05:44

It seems that there is a problem with geolocation on Mozilla Firefox 24+ based on

https://support.mozilla.org/en-US/questions/984520

Run Firefox with administrative privileges (especially on Windows).

查看更多
登录 后发表回答