我可以知道如何获取当前用户位置并计算位置B之间的距离(以公里为单位)吗?
我尝试了以下代码,但似乎不起作用。
<?php
echo "<script type = 'text/javascript'>
function showPosition(){
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
});
} else {
alert(\"Sorry, your browser does not support HTML5 geolocation.\");
}
}
</script>";
$point1 = array("lat" => $latitude, "long" => $longitude);
$point2 = array("lat" => $row_Merchant['latitude'], "long" => $row_Merchant['longitude']);
$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); // Calculate distance in kilometres (default)
echo "$km km";
?>
<?php
function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2)
{
// Calculate the distance in degrees
$degrees = rad2deg(acos((sin(deg2rad($point1_lat)) * sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat)) * cos(deg2rad($point2_lat)) * cos(deg2rad($point1_long - $point2_long)))));
// Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles)
switch ($unit) {
case 'km':
$distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km)
break;
case 'mi':
$distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles)
break;
case 'nmi':
$distance = $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles)
}
return round($distance, $decimals);
}
?>
如何在$ point1 = array(“ lat” => $ latitude,“ long” => $ longitude)中传递纬度和经度值;这样我就可以计算出用户位置和位置B之间的距离?
请帮助。谢谢。
答案 0 :(得分:1)
Eddy当前,您打算将javascript变量分配给PHP变量,这不是正确的处理方式,因为在客户端,一种在服务器端。 Reference.
我已经更改了您的代码,以查找用户位置与商家位置之间的距离。
<?php
$row_Merchant = [
'latitude' => 47.683277,
'longitude' => 3.210482
]; //Change this with database value
?>
<script type = 'text/javascript'>
var latitude = "";
var longitude = "";
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344; }
if (unit=="N") { dist = dist * 0.8684; }
return dist;
}
function showPosition(){
console.log('hhhh');
if(navigator.geolocation) {
console.log('hhhh');
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log(latitude+','+longitude+','+ merch_latitude+','+ merch_longitude);
dist = distance(latitude, longitude, merch_latitude, merch_longitude, 'K');
console.log(dist); //final distance present in dist variable
});
} else {
alert("Sorry, your browser does not support HTML5 geolocation.");
}
}
var merch_longitude = <?php echo $row_Merchant['longitude']; ?>;
var merch_latitude = <?php echo $row_Merchant['latitude']; ?>;
showPosition();
</script>
在上面的代码中,您会将数据库值传递给当前已硬编码的$ row_Merchant。此值将设置为javascript,使用javascript我们也将获取用户的坐标。使用这些坐标,我们的javascript函数将计算距离。
距离函数采用5个参数lat1,long1,lat2,long2,unit。
默认单位是“英里”,您可以将K表示KM,将N表示NM。