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