我需要用户的纬度和经度使用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提出任何其他代码来获取用户的经纬度。 谢谢。
在谷歌浏览器它工作在https上。你可以在Mozilla中检查它。 Mozilla支持http和https –
同样的错误Mozila Developer Editon。 “地理位置请求只能在安全的上下文中完成。” – TarangP