PHP - 两个位置之间的距离,接近

时间:2013-06-15 16:20:36

标签: php geolocation coordinates distance

根据:http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/

我要检查两个给定位置之间的距离。

我的dubt是这两种方法中的哪一种“更好”,为什么如果?

最前一页:

<?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);
?>

1 个答案:

答案 0 :(得分:1)

余弦计算法基于球形地球; Haversine是一个很好的圆计算:假设地球是一个椭圆而不是一个球体,那么Haversine更准确......虽然Vincenty算法比Haversine更准确的大圆计算,但计算速度较慢。余弦计算是一种快速近似,在短距离内相当准确; Haversine在较长距离上更准确,因为它更合理地近似于地球的曲率,但对于交叉极地大圆来说并不是特别准确。

您可以找到差异的良好解释here