我在stackoverflow(Check if coordinates is within a specific distance from other coordinates)的另一个问题中找到了这个函数,它返回的全部是0.00。我不知道为什么它不起作用,因为它似乎适用于其他人。拜托,我看不出有什么问题,是吗?
function calculate_distance($busCoord, $spot){
$lat1 = $busCoord[0]; //40.4167754
$lng1 = $busCoord[1]; //3.7037901999999576
$lat2 = $spot[0]; //40.466173
$lng2 = $spot[1]; //3.6706590000000006
$pi = 3.1414926;
$rad = doubleval($pi/180.0);
$lon1 = $lng1*$rad;
$lat1 = $lat1*$rad;
$lon2 = $lng2*$rad;
$lat2 = $lat2*$rad;
$theta = $lng2 - $lng1;
$dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
if ($dist < 0)
$dist += $pi
$miles = doubleval($dist * 69.1);
$inches = doubleval($miles * 63360);
$km = doubleval($dist * 114.1666667);
$dist = sprintf( "%.2f",$dist);
$miles = sprintf( "%.2f",$miles);
$inches = sprintf( "%.2f",$inches);
$km = sprintf( "%.2f",$km);
//Here you can return whatever you please
return $km;
}