0

I've tested the following functions for measuring the distance between two coordinates:

<?php

$var1 = vincentyGreatCircleDistance(500359,0054253,583838,0030412,6371000);

echo "$var1" ;
$var2 = distance(500359,0054253,583838,0030412,'K');
echo "<br>$var2" ;

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

      $theta = $lon1 - $lon2;
      $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
      $dist = acos($dist);
      $dist = rad2deg($dist);
      $miles = $dist * 60 * 1.1515;
      $unit = strtoupper($unit);

      if ($unit == "K") {
        return ($miles * 1.609344);
      } else if ($unit == "N") {
          return ($miles * 0.8684);
        } else {
            return $miles;
          }
    }




  function vincentyGreatCircleDistance(
  $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
    pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  return $angle * $earthRadius;
}

?>

but first function returns 5120227.5400291 and second function returns 5119.9812014323 ! I'm confused because below site returns 5363 km:

http://www.movable-type.co.uk/scripts/latlong.html

which one of them is correct ?!!!


source :

first function :

http://www.geodatasource.com/developers/php

second function :

Measuring the distance between two coordinates in PHP

Community
  • 1
  • 1
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • The first one looks like it's missing a `/1000` somewhere, and the second one is assuming that the Earth is a sphere, which it isn't. The reference site is probably using a more accurate model of the Earth, hence the difference - especially over such a large distance. – Niet the Dark Absol Sep 22 '14 at 13:50
  • I would like to use one of then ! but.. which ?! – S.M_Emamian Sep 22 '14 at 13:56
  • They are both incorrect according to the standard that you indicated in your question. Why do you wish to use incorrect code? If you consider the '5363 km' value to be the correct one, you will need to determine what algorithm is used to generate that number and imitate it. – George Cummins Sep 22 '14 at 14:09

0 Answers0