navigator.geolocation.getCurrentPosition回调在Firefox 10上不起作用 [英] navigator.geolocation.getCurrentPosition Callbacks won't work on Firefox 10

查看:1016
本文介绍了navigator.geolocation.getCurrentPosition回调在Firefox 10上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用Geolocation API的应用。我似乎无法在Firefox 10上使用一段非常简单的代码。以下是代码:

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

因此,例如,在Chrome中,运行页面后,我会被问到是否要分享我的位置,点击是后会提醒我它有效。现在在Firefox 10中,它会让我分享我的位置,点击分享后它什么也没做......我一直试图让回调运行任何类型的代码,但没有运气。这是Firefox的错误还是我做错了什么?我在这里有一个代码测试示例: http://dev-hub.com/geolocation.html

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.

编辑---
我的操作系统是Windows 7 64位

Edit--- My OS is windows 7 64bit

推荐答案

好吧我发现问题确实是Firefox,并且它在所有平台上都无法可靠或同等地运行。查看 http://dev.w3.org/geo/api/spec-source .html 我找到了以下选项:

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

正如您在此处所看到的,已添加超时:5000,这意味着如果出于某种原因浏览器需要超过5000毫秒(5秒)然后抛出超时错误(这是错误代码3)。所以现在每当Firefox不工作时它至少运行错误回调,我收到一条警告消息发生错误。错误代码:3。

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".

显然,超时的默认值是无限的,所以它永远不会超时...... Chrome是100%可靠但Firefox在我的机器上大约10%可靠,这是非常令人失望的。在我的另一台运行Windows XP且位于同一网络上的计算机上,Firefox似乎100%可靠。

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.

这篇关于navigator.geolocation.getCurrentPosition回调在Firefox 10上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆