Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function codexworldGetDistanceOpt($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
  2. {
  3. $rad = M_PI / 180;
  4. //Calculate distance from latitude and longitude
  5. $theta = $longitudeFrom - $longitudeTo;
  6. $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
  7.  
  8. return acos($dist) / $rad * 60 * 1.852;
  9. }
  10. ///
  11.  
  12. function circle_distance($lat1, $lon1, $lat2, $lon2) {
  13. $rad = M_PI / 180;
  14. return acos(sin($lat2*$rad) * sin($lat1*$rad) + cos($lat2*$rad) * cos($lat1*$rad) * cos($lon2*$rad - $lon1*$rad)) * 6371;// Kilometers
  15. }
  16.  
  17. ///
  18. function getDistance($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) {
  19. // Calculate the distance in degrees
  20. $degrees = rad2deg(acos((sin(deg2rad($point1_lat))*sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat))*cos(deg2rad($point2_lat))*cos(deg2rad($point1_long-$point2_long)))));
  21.  
  22. // Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles)
  23. switch($unit) {
  24. case 'km':
  25. $distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km)
  26. break;
  27. case 'mi':
  28. $distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles)
  29. break;
  30. case 'nmi':
  31. $distance = $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles)
  32. }
  33. return round($distance, $decimals);
  34. }
  35.  
  36.  
  37.  
  38.  
  39. ////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement