Calculate the Distance Between Two Coordinates (latitude, longitude)

  1. function distance ( $lat1 , $lng1 , $lat2 , $lng2 , $miles = true )
  2. {
  3. $pi80 = M_PI / 180;
  4. $lat1 *= $pi80;
  5. $lng1 *= $pi80;
  6. $lat2 *= $pi80;
  7. $lng2 *= $pi80;
  8.  
  9. $r = 6372.797; // mean radius of Earth in km
  10. $dlat = $lat2 - $lat1;
  11. $dlng = $lng2 - $lng1;
  12. $a = sin ( $dlat / 2 ) * sin ( $dlat / 2 ) + cos ( $lat1 ) * cos ( $lat2 ) * sin ( $dlng / 2 ) * sin ( $dlng / 2 );
  13. $c = 2 * atan2 ( sqrt ( $a ) , sqrt ( 1 - $a ) );
  14. $km = $r * $c;
  15.  
  16. return ( $miles ? ( $km * 0.621371192 ) : $km );
  17. }

你可能感兴趣的:(long)