简体   繁体   English

网页上的地理位置无法正常工作

[英]Geolocation on webpage not working

I have used the script below many times before although recently I have not been able to get it to work. 我曾经在下面多次使用该脚本,尽管最近我无法使其正常工作。 When I try and run it I get the error Location information is unavailable. 当我尝试运行它时,出现错误Location information is unavailable.

I am not sure why. 我不知道为什么。 I have checked update notes of browsers etc. to see if this is a function that has been removed and I can't find reference to this being the case. 我检查了浏览器等的更新说明,以查看这是否已被删除,而我找不到这种情况的引用。

If anyone has any idea as to what I am doing wrong, I would be most grateful if you are able to point me in the right direction. 如果有人对我做错了什么有任何想法,如果您能指出正确的方向,我将不胜感激。

Thanks in advance! 提前致谢!

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

I assume that You are using Chrome and Your website doesn't have SSL right? 我认为您使用的是Chrome浏览器,而您的网站没有SSL权限吗? Unfortunately gelocation doesn't work on Chrome in that conditions. 不幸的是,在这种情况下,地理位置无法在Chrome上运行。 You must test on other browser ie. 您必须在其他浏览器上进行测试,即。 Firefox or get SSL for Your website. Firefox或为您的网站获取SSL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM