【发布时间】:2018-03-26 22:19:20
【问题描述】:
我需要使用 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
而Same code works here..else 建议任何其他代码来获取用户纬度和经度。 谢谢。
【问题讨论】:
-
在 google chrome 中它在 https 上工作。你可以在Mozilla中检查它。 Mozilla 同时支持 http 和 https
-
在 Mozilla 开发者版中出现同样的错误。 “地理定位请求只能在安全的环境中完成。”
标签: javascript php geolocation google-geolocation