Where did I go wrong with Haversine calculation in php? -


i have 3 methods looks same result different...

class geocalculations {      const earth_radius = 6371000;      public function degtorad($deg)     {         return $deg * pi() / 180;     }      public function haversinedistance($lat_long_1, $lat_long_2) {         $lat_1 = $this->degtorad($lat_long_1->lat);         $lng_1 = $this->degtorad($lat_long_1->lng);         $lat_2 = $this->degtorad($lat_long_2->lat);         $lng_2 = $this->degtorad($lat_long_2->lng);          $d_lat = $lat_2 - $lat_1;         $d_lng = $lng_2 - $lng_2;          echo "-----------------\n";         var_dump($lat_1);         var_dump($lng_1);         var_dump($lat_2);         var_dump($lng_2);         var_dump($d_lat);         var_dump($d_lng);         echo "-----------------\n";          $cord_length = pow(sin($d_lat / 2), 2) + cos($lat_1) * cos($lat_2) * pow(sin($d_lng / 2), 2);         $central_angle = 2 * atan2(sqrt($cord_length), sqrt(1 - $cord_length));          return self::earth_radius * $central_angle;     }      public function haversinedistance2($lat_long_1, $lat_long_2)     {         $lat_1 = deg2rad($lat_long_1->lat);         $lng_1 = deg2rad($lat_long_1->lng);         $lat_2 = deg2rad($lat_long_2->lat);         $lng_2 = deg2rad($lat_long_2->lng);          $d_lat = $lat_2 - $lat_1;         $d_lng = $lng_2 - $lng_2;          echo "-----------------\n";         var_dump($lat_1);         var_dump($lng_1);         var_dump($lat_2);         var_dump($lng_2);         var_dump($d_lat);         var_dump($d_lng);         echo "-----------------\n";          $angle = 2 * asin(sqrt(pow(sin($d_lat / 2), 2) + cos($lat_1) * cos($lat_2) * pow(sin($d_lng / 2), 2)));          return $angle * self::earth_radius;     }      public function haversinedistance3($lat_long_1, $lat_long_2)     {         $latfrom = deg2rad($lat_long_1->lat);         $lonfrom = deg2rad($lat_long_1->lng);         $latto = deg2rad($lat_long_2->lat);         $lonto = deg2rad($lat_long_2->lng);          $latdelta = $latto - $latfrom;         $londelta = $lonto - $lonfrom;          echo "-----------------\n";         var_dump($latfrom);         var_dump($lonfrom);         var_dump($latto);         var_dump($lonto);         var_dump($latdelta);         var_dump($londelta);         echo "-----------------\n";          $angle = 2 * asin(sqrt(pow(sin($latdelta / 2), 2) +cos($latfrom) * cos($latto) * pow(sin($londelta / 2), 2)));         return $angle * self::earth_radius;     } }  $new = new geocalculations();  $circle_1 = (object) array('lat' => 36.06228, 'lng' => -79.60886); $circle_2 = (object) array('lat' => 36.10926, 'lng' => -79.54423);  var_dump($new->haversinedistance($circle_1, $circle_2)); var_dump($new->haversinedistance2($circle_1, $circle_2)); var_dump($new->haversinedistance3($circle_1, $circle_2)); 

it prints:

-----------------
float(0.6294055217761)
float(-1.3894367207592)
float(0.63022547745869)
float(-1.3883087144636)
float(0.00081995568258686)
float(0)
-----------------
float(5223.9376537609)
-----------------
float(0.6294055217761)
float(-1.3894367207592)
float(0.63022547745869)
float(-1.3883087144636)
float(0.00081995568258697)
float(0)
-----------------
float(5223.9376537616)
-----------------
float(0.6294055217761)
float(-1.3894367207592)
float(0.63022547745869)
float(-1.3883087144636)
float(0.00081995568258697)
float(0.0011280062955641)
-----------------
float(7811.4545246093)

the third result correct, can't understand wrong first two?

the first 2 have:

$d_lng = $lng_2 - $lng_2; 

rather than:

$d_lng = $lng_2 - $lng_1; 

(equivalent line in third example):

$londelta = $lonto - $lonfrom; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -