我需要找到当前位置之间的距离,为此,我正在使用getCurrentPosition()和数据库表中的其他地址。
<script>
var latitude;
var longitude;
function showPosition(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
};
navigator.geolocation.getCurrentPosition(showPosition);
</script>
</script>
<?php
$store = $row['store'];
$latitudeFrom = ;
$longitudeFrom = ;
$sql = "SELECT * FROM stores WHERE store = '$store'";
$result = mysqli_query($connection, $sql); //die($sql);
$r = mysqli_fetch_array($result);
$latitudeTo = $r['latitude'];
$longitudeTo = $r['longitude'];
//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) + cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$distance = ($miles * 1.609344).' km';
?>
如何在php代码中使用地址的经度和纬度来计算距离?