navigator.geolocation.getCurrentPosition无法使用Firefox

时间:2014-12-08 22:04:06

标签: javascript html firefox geolocation

当我拒绝透露我的位置时,我刚开始发现navigator.geolocation.getCurrentPosition不适用于Firefox。即它不像以前那样返回错误。它适用于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>
&#13;
&#13;
&#13;

如果我允许访问我的位置,这适用于Firefox。如果我拒绝访问,则不会调用showPosition或showError函数。什么都没发生。我用Mac和PC Firefox试过这个并得到同样的问题。适用于Safari和Chrome。但

1 个答案:

答案 0 :(得分:0)

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