这是一个非常有用的距离计算函数,利用纬度和经度计算从 A 地点到 B 地点的距离。该函数可以返回英里,公里,海里三种单位类型的距离。
- 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, "k")." kilometers";
复制代码
|