地理位置 getCurrentPosition() 和 watchPosition() 不适用于不安全的来源

Geo location getCurrentPosition() and watchPosition() not work on insecure origins

我需要使用 php 的用户的纬度和经度。试试下面的代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of HTML5 Geolocation</title>
<script type="text/javascript">
    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>                            

但是当我 运行 服务器中的这段代码时,我在获取用户纬度和经度时发现了这个警告错误。

getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS

while Same code works here..else 建议任何其他代码来获取用户纬度和经度。 谢谢。

地理定位 API 从 Chrome 50 的不安全来源中删除。

此更改自 Chrome50(2016 年 4 月 20 日太平洋标准时间中午 12 点)起生效。

Chrome 的开发人员工具控制台自版本 44(2015 年 7 月 21 日发布)以来一直提供警告。 已经有许多 public 公告描述了我们进行此更改的理由(和讨论):

打算通过 HTTP 弃用一组强大的功能(2015 年 2 月) Intent to deprecate Geolocation API over HTTP(2015 年 11 月) Chrome 开发者峰会(2016 年 11 月) Chrome Beta 频道发布博客(2016 年 3 月 17 日) Chrome 状态网站 还有许多其他来源强调了这一点:Mobiforge(2016 年 1 月 26 日)、Wired(2016 年 3 月 17 日)、VentureBeat(2016 年 4 月 13 日)。

Read More Documentatin here 。因此,如果没有 HTTPS,就无法使用 GeoLocation。