html - navigator.geolocation.getCurrentPosition 回调在 Firefox 10 上不起作用

标签 html firefox geolocation

我正在构建一个使用 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');
        }
    };

因此,例如,在 chrome 中,在运行该页面后,系统会询问我是否要共享我的位置,在单击"is"后,它会提醒我“有效”。现在在 Firefox 10 中,它会要求我分享我的位置,点击分享后它什么都不做……我一直试图让回调运行任何类型的代码,但没有成功。这是 Firefox 的错误还是我做错了什么?我这里有一个用于测试的代码示例:http://dev-hub.com/geolocation.html .

编辑--- 我的操作系统是 windows 7 64bit

最佳答案

好吧,我发现问题确实出在 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');
        }
    };

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

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

关于html - navigator.geolocation.getCurrentPosition 回调在 Firefox 10 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215662/

相关文章:

geolocation - 有没有一种方法可以获得不包含国家/地区的世界形状文件?

javascript - 垂直滚动某些部分进入 View 时如何隐藏/显示固定元素?

html - 在不使用 HTML 5 标签的情况下使用 HTML 5 Doctype `<!DOCTYPE html>` 是否有任何优点?

javascript - IONIC 和 angularjs : Nothing happends when using angular-ui-router in navigation component

javascript - Google map 用例在 Firefox 中存在 JS 错误

Firefox 锁定 places.sqlite

php - 使用 Shapefile 数据确定经度/纬度的邻域

html - 如何防止嵌入的视频链接到另一个网页

firefox - CSS3 过渡/悬停效果在 Firefox 中不起作用; Firefox 错误?

python - 我应该如何对 1,100,000 行坐标信息进行地理定位?