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

时间:2017-10-15 07:12:46

标签: javascript php geolocation google-geolocation

我需要使用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()和watchPosition()不再适用于不安全   起源。要使用此功能,您应该考虑切换您的   应用程序到安全源,例如HTTPS

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

1 个答案:

答案 0 :(得分:2)

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

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

自第44版(2015年7月21日发布)以来,Chrome的开发者工具控制台一直在提供警告。 已经有一些公告声明了我们为什么要做出这种改变的理由(和讨论):

意图通过HTTP弃用一组强大的功能(2015年2月) 意图通过HTTP弃用Geolocation API(2015年11月) Chrome Dev Summit(2016年11月) Chrome测试版频道发布博客(2016年3月17日) Chrome状态网站 还有一些其他消息来源强调了这一点:Mobiforge(2016年1月26日),有线(2016年3月17日),VentureBeat(2016年4月13日)。

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

相关问题