且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

getCurrentPosition()在Firefox中不起作用

更新时间:2022-12-18 09:50:52

实际上,我在Chrome 32(它工作正常)和Firefox 26没有运气)。



我尝试在Firefox上启用地理位置,但仍然无法正常工作。



我浏览过情侣网站上的谷歌,最后我明白的是Firefox 26& 27在检测地理位置时存在一个错误。

为了测试功能,您可以运行这个 LIVE DEMO 这是一个普通的javascript。

 初始化(); 

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


function displayError(positionErr){
alert(error);


函数initialize(){
if(!(navigator.geolocation =='undefined')){
navigator.geolocation.getCurrentPosition(displayLocation,displayError ,{
timeout:10000
});
} else {
alert('Geolocation unsupported');
}
}


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 :)

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');
    }
}