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

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

I need user's Lattitude and longitude using php. try following code.

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

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

but when i run this code in server i found this warning error when i fetch User Lattitude and Longitude.

getCurrentPosition() 和 watchPosition() 不再适用于不安全起源.要使用此功能,您应该考虑切换您的应用到安全源,例如 HTTPS

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

虽然 同样的代码在这里工作..else 建议获取用户纬度和经度的任何其他代码.谢谢.

while Same code works here..else suggest any other code to get user Latitude and longitude. thanks.

推荐答案

Geolocation API 从 Chrome 50 中的 Unsecured Origins 中移除.

Geolocation API Removed from Unsecured Origins in Chrome 50.

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

This change is effective as of Chrome 50 (12PM PST April 20 2016).

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

Chrome's developer tools console has been providing warnings since version 44 (released July 21 2015). There have been a number of public announcements that describe the rationale (and discussion) of why we are making this change:

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

Intent to deprecate set of powerful features over HTTP (Feb 2015) Intent to deprecate Geolocation API over HTTP (Nov 2015) Chrome Dev Summit (Nov 2016) Chrome Beta Channel release blog (March 17, 2016) Chrome Status website There have been a number of other sources that have highlighted this: Mobiforge (Jan 26, 2016), Wired (March 17, 2016), VentureBeat (April 13, 2016).

在这里阅读更多文档 .所以没有 HTTPS 就无法使用 GeoLocation.

Read More Documentatin here .So thats not possible to use GeoLocation Without HTTPS.

相关文章