我要检查两个给定位置之间的距离。
我的问题是这两种方法中的哪一种“更好”,为什么?
首先:
<?php
$earth_radius = 3960.00; # in miles
$lat_1 = "47.117828";
$lon_1 = "-88.545625";
$lat_2 = "47.122223";
$lon_2 = "-88.568781";
$delta_lat = $lat_2 - $lat_1 ;
$delta_lon = $lon_2 - $lon_1 ;
# Spherical Law of Cosines
function distance_slc($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;
global $delta_lat;
global $delta_lon;
$distance = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($delta_lon)) ;
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
$distance = round($distance, 4);
return $distance;
}
$slc_distance = distance_slc($lat_1, $lon_1, $lat_2, $lon_2);
?>
第二
<?php
$earth_radius = 3960.00; # in miles
$lat_1 = "47.117828";
$lon_1 = "-88.545625";
$lat_2 = "47.122223";
$lon_2 = "-88.568781";
$delta_lat = $lat_2 - $lat_1 ;
$delta_lon = $lon_2 - $lon_1 ;
function distance_haversine($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;
global $delta_lat;
global $delta_lon;
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);
return $distance;
}
$hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);
?>