Advertisement
krot

geo distance

Apr 10th, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.48 KB | None | 0 0
  1. function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  2.       $theta = $lon1 - $lon2;
  3.       $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  4.       $dist = acos($dist);
  5.       $dist = rad2deg($dist);
  6.       $miles = $dist * 60 * 1.1515;
  7.       $unit = strtoupper($unit);
  8.  
  9.       if ($unit == "K") {
  10.         return ($miles * 1.609344);
  11.       } else if ($unit == "N") {
  12.           return ($miles * 0.8684);
  13.         } else {
  14.             return $miles;
  15.           }
  16.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement