网页上的地理位置不起作用



我以前多次使用下面的脚本,尽管最近我无法让它工作。当我尝试运行它时,出现错误Location information is unavailable.

我不知道为什么。我已经检查了浏览器等的更新说明,以查看这是否是已删除的功能,但我找不到这种情况的参考。

如果有人知道我做错了什么,如果您能够指出我正确的方向,我将不胜感激。

提前感谢!

<!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>

我假设您使用的是Chrome,而您的网站没有SSL对吗?不幸的是,在这种情况下,妊娠在Chrome上不起作用。您必须在其他浏览器上进行测试,即。火狐或为您的网站获取SSL。

最新更新