2

によると: http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/

指定された 2 地点間の距離を確認します。

私の疑問は、これら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 ;

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

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