【问题标题】:navigator.geolocation.getCurrentPosition not working with Firefoxnavigator.geolocation.getCurrentPosition 不适用于 Firefox
【发布时间】:2014-12-08 22:04:06
【问题描述】:

当我拒绝透露我的位置时,我刚刚开始发现 navigator.geolocation.getCurrentPosition 不适用于 Firefox。 IE。它不会像以前那样返回错误。它在 Safari 和 Chrome 上运行良好,但在 Firefox 33.1 上运行良好

我在运行 w3schools 示例代码 (http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error) 时可以看到这个问题,如下所示:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

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

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

<script>
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;
    }
}
</script>

</body>
</html>

如果我允许访问我的位置,这适用于 Firefox。如果我拒绝访问,则不会调用 showPosition 或 showError 函数。没发生什么事。我已经用 Mac 和 PC Firefox 尝试过这个并遇到同样的问题。不过在 Safari 和 Chrome 上运行良好。

【问题讨论】:

  • 适用于 FF 30.0,下载最新
  • 也适用于 FF 34.0 - 可能是 33.1 的问题?您看到的错误是什么?传递给 showError 的对象对我来说是 PositionError { code: 1, message: "User denied geolocation prompt" }
  • 我根本没有看到错误,就好像函数没有返回
  • 我的问题是,如果我拒绝访问,showPosition 和 showError 函数都不会被调用。

标签: javascript html firefox geolocation


【解决方案1】:

我刚刚发现这是 Firefox 的一个特性,或者我更喜欢称之为 bug。详细在这里:https://bugzilla.mozilla.org/show_bug.cgi?id=675533

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2017-08-11
    • 2017-01-16
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多