0
我有一个使用Google Maps API和JavaScript地理定位系统计算距离的PHP脚本。从PHP获取JavaScript函数输出
我想获得当前用户的坐标,我的PHP代码,例如:
$user_location = array(latitude, longitude);
截至目前,我想是这样的:
location.php:
$contents = file_get_contents('user_loc.html');
if ($contents != "false") {
$user_location = explode(',', $contents);
}
// Do stuff with coordinates
user_loc.html:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
navigator.geolocation.getCurrentPosition(
function (position) {
console.log(position.coords.latitude);
document.write(position.coords.latitude + ',' + position.coords.longitude);
},
function (error) {
document.write("false");
},
{
timeout: (5 * 1000),
maximumAge: (1000 * 60 * 15),
enableHighAccuracy: true
}
);
</script>
</head>
<body>
</body>
</html>
我很确定这不是最好的方法来做到这一点。有什么建议么?
您必须使用js在客户端上运行才能获取地理位置,然后将其发送回服务器。或者您可以根据请求者的IP获取地理位置,但这可能不太可靠。 – carter
file_get_contents将获得文件的字符串输出,而不是您正在查找的结果。您需要访问该页面,然后在页面上,您可以使用lat和long进行POST到location.php,并根据需要进行处理 – Fallenreaper