2

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

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

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

首先:

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

1 回答 1

1

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

您可以在此处找到对差异的很好解释

于 2013-06-15T16:49:51.723 回答