【问题标题】:Issue with Geolocation HTML5地理位置 HTML5 的问题
【发布时间】:2021-01-26 11:51:05
【问题描述】:

我有一个用于地理定位的简单代码,通过向用户询问许可并允许在他按下一个简单的按钮时获取他的位置,当我在桌面上运行文件时它可以工作并且按钮功能,但是如果我上传它到一个像免费主机一样的主机 [http://darkdays.byethost14.com/cc.html] 按钮将不起作用意味着没有任何东西弹出!

感谢您的帮助

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Current Position</title>
<script>
    function showPosition() {
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
                document.getElementById("result").innerHTML = positionInfo;
            });
        } else {
            alert("Sorry, your browser does not support HTML5 geolocation.");
        }
    }
</script>
</head>
<body>
    <div id="result">
        <!--Position information will be inserted here-->
    </div>
    <button type="button" onclick="showPosition();">Show Position</button>
</body>
</html>

【问题讨论】:

  • 这里的一切都在浏览器中运行,所以只要服务器在为页面提供服务,它就必须与浏览器设置相关。如果页面是从远程站点提供的,您的浏览器可能设置为不允许位置请求。
  • 我已经在不同的电脑上试过了,在手机上也有同样的问题! @RiggsFoll
  • 也许你应该lookup Secure Context也许这是你的问题。本地交付的资源,例如具有 127.0.0.1 URL、localhost 和 http://*.localhost URL(例如 dev.whatever.localhost)和 file:// URL 的资源也被视为已安全交付。
  • 我不确定您获得免费托管网站的 ssl 证书的可能性有多大
  • 您尝试过什么方法来解决这个问题?浏览器控制台是否记录了任何内容?另外,这和 PHP 有什么关系?

标签: javascript php html


【解决方案1】:

网站需要有 ssl 证书。只有这样,您才能使用任何 navigator 属性。它也可用作本地文件。 大多数(可能是所有)免费托管服务不免费提供 ssl 证书,因此您使用的是http 协议而不是https

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多