2017-10-15 60 views
0

我需要用户的纬度和经度使用PHP。尝试下面的代码。地理位置getCurrentPosition()和watchPosition()不起作用在不安全的起源

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

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

getCurrentPosition()和watchPosition()没有在不安全的 起源不再起作用。要使用此功能,你应该考虑你的 应用切换到一个安全的起点,如HTTPS

Same code works here.的.else提出任何其他代码来获取用户的经纬度。 谢谢。

+0

在谷歌浏览器它工作在https上。你可以在Mozilla中检查它。 Mozilla支持http和https –

+0

同样的错误Mozila Developer Editon。 “地理位置请求只能在安全的上下文中完成。” – TarangP

回答

1

地理位置API在Chrome 50.

这种变化作为铬50是有效的(12PM PST 2016年4月20日),从起源不安全移除。

自从版本44(2015年7月21日发布)以来,Chrome的开发人员工具控制台一直提供警告。 已经有许多描述为什么我们正在这一变化的理由(和讨论)公示的:

意图弃用通过HTTP设置的强大功能(2015年2月) 意图通过HTTP贬低地理位置API (2015年11月) Chrome Dev Summit(2016年11月) Chrome Beta频道发布博客(2016年3月17日) Chrome状态网站 已有多个其他来源强调了这一点:Mobiforge(2016年1月26日),有线(2016年3月17日),VentureBeat(2016年4月13日)。

Read More Documentatin here。因此,不可能使用不带HTTPS的GeoLocation。

相关问题