Html navigator.geolocation.getCurrentPosition 回调不适用于 Firefox 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9215662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
navigator.geolocation.getCurrentPosition Callbacks won't work on Firefox 10
提问by Zaptree
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:
我正在构建一个使用 Geolocation API 的应用程序。我似乎无法得到一段非常简单的代码来在 Firefox 10 上工作。这是代码:
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.
因此,例如,在 chrome 中,在运行页面后,我会被问到是否要共享我的位置,单击“是”后,它会提醒我“它有效”。现在在 Firefox 10 中,它会要求我分享我的位置,点击分享后它什么也不做......我一直试图让回调运行任何类型的代码,但没有运气。这是 Firefox 的错误还是我做错了什么?我在这里有一个用于测试的代码示例:http: //dev-hub.com/geolocation.html。
Edit--- My OS is windows 7 64bit
编辑---我的操作系统是 windows 7 64bit
回答by Zaptree
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.htmlI found the following option to add:
好吧,我发现问题确实出在 Firefox 上,并且它在所有平台上都无法可靠或同等地工作。查看http://dev.w3.org/geo/api/spec-source.html我发现添加以下选项:
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".
正如您在此处看到的,已添加 timeout:5000 这意味着如果由于某种原因浏览器花费的时间超过 5000 毫秒(5 秒),则抛出超时错误(即错误代码 3)。因此,现在每当 Firefox 不工作时,它至少会运行错误回调,并且我会收到“发生错误。错误代码: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.
显然超时的默认值是无限的,所以它永远不会超时...... Chrome 是 100% 可靠的,但 Firefox 在我的机器上大约是 10% 可靠,这非常令人失望。在我的另一台运行 Windows XP 且在同一网络上的计算机上,Firefox 似乎是 100% 可靠的。
回答by user3307754
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} );
hope it helps!
希望能帮助到你!