[英]how to get distance between points using latitude and longitude using php?
I am trying to get distance between two location. 我试图获得两个位置之间的距离。
Here one location is user input and another location is my store location. 这里一个位置是用户输入,另一个位置是我的商店位置。 I want to show the the distance between there input location to my store location.
我想显示输入位置到我的商店位置之间的距离。
To get user location we use form, where user share there zip code and we convert that zip to longitude and latitude. 为了获得用户位置,我们使用表格,用户在其中共享邮政编码,然后将其转换为经度和纬度。 And in my data base i stored the store location and its respective longitude and latitude.
然后在我的数据库中存储了商店位置及其各自的经度和纬度。
Here in this case can any one help me to get distance. 在这种情况下,任何人都可以帮助我拉开距离。
Thanks 谢谢
GeoDataSource team develop a very useful function to achieve this: GeoDataSource团队开发了一个非常有用的功能来实现此目的:
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.