javascript - 为什么 HTML5 地理定位在不同的 Web 浏览器中出现不一致的错误行为

标签 javascript html google-chrome firefox

我一直在测试一个简单的code fragment它使用 HTML5 地理定位功能返回经度和纬度值:

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}
<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

我直接从“尝试一下”部分在 Google Chrome 中测试了代码,它返回了“error.PERMISSION_DENIED”。但当我将它部署在 localhost 下的 XAMPP 中时,它起作用了。请注意,我已将 Google Chrome 设置为基于此 documentation 共享位置详细信息.

但是,即使部署在 XAMPP 中并且当我同意在 Firefox 中共享我的位置时,此代码也会在 Firefox 中返回“error.POSITION_UNAVAILABLE”。

是什么导致 Google Chrome 中(直接调用时以及在本地主机下部署时)和通过 Firefox 访问时出现不一致的行为?

最佳答案

关于javascript - 为什么 HTML5 地理定位在不同的 Web 浏览器中出现不一致的错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38853970/

相关文章:

javascript - AngularJS 函数在 javascript 加载后不应用?

Javascript 函数返回 '[object Object]' 而不是哈希值

javascript - 安装进度条

javascript - Gmail 似乎捕获所有键盘事件。有什么办法解决这个问题吗?

c++ - 如何在 Chrome Native Messaging Extension 的 native 应用程序中使用 native 主机消息

javascript - 防止jQuery UI Datepicker(内联类型)选择今天的日期

jquery - 当我重新加载页面时,标签链接 CSS 恶化

javascript - Magic Line CSS/Javascript 代码 - 添加子导航

html - 带有子菜单 CSS HTML 的下拉菜单

javascript - 如何转发到浏览器的主页(谷歌浏览器)