为什么HTML5地理定位在不同的浏览器中有不一致的错误行为?

Why does HTML5 Geolocation have inconsistent errorneous behavior in different web browsers

本文关键字:不一致 错误 浏览器 为什么 定位 HTML5      更新时间:2023-09-26

我一直在测试一个简单的代码片段,它使用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>

我测试了代码在谷歌浏览器直接从"尝试一下"部分,它返回'error.PERMISSION_DENIED'。但是,当我在本地主机下的XAMPP中部署它时,它工作了。请注意,我已经设置了谷歌Chrome浏览器共享基于此文档的位置详细信息。

但是这段代码返回'error '。POSITION_UNAVAILABLE'在Firefox中,即使在XAMPP中部署,当我同意在Firefox中共享我的位置。

是什么原因导致这种不一致的行为在谷歌浏览器(当直接调用和部署在本地主机),并通过Firefox访问时?

共享位置需要安全连接。更多:https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins