我想通过 php 脚本获取计算机用户的地理位置(纬度和经度)。我用的是这个
<?php
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
但问题是这使用了真实地址,我只想让用户登录,请求他允许跟踪他(允许位置),如果他接受则获取所需的值(经度和纬度)。可以通过 IP 地址 或其他方式来查明他的位置,但问题是我希望此方法实际上 在代理和骗子。关于如何做到这一点的任何想法?非常感谢!
最佳答案
我认为这就是您想要的。简单易用。感谢 HTML5 see source here
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
关于php - 如何通过IP地址或精确定位获取用户的经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769064/