Posted By

sendoa on 10/14/10


Tagged


Versions (?)

Calcular distancias entre puntos geográficos


 / Published in: PHP
 

URL: http://www.phpsnippets.info/calculate-distances-in-php

  1. function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  2.  
  3. $theta = $lon1 - $lon2;
  4. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  5. $dist = acos($dist);
  6. $dist = rad2deg($dist);
  7. $miles = $dist * 60 * 1.1515;
  8. $unit = strtoupper($unit);
  9.  
  10. if ($unit == "K") {
  11. return ($miles * 1.609344);
  12. } else if ($unit == "N") {
  13. return ($miles * 0.8684);
  14. } else {
  15. return $miles;
  16. }
  17. }
  18.  
  19. //USAGE
  20.  
  21. //Miles
  22. echo distance(32.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br/>";
  23.  
  24. //Kilometers
  25. echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br/>";
  26.  
  27. //Nautical miles
  28. echo distance(32.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles<br/>";

Report this snippet  

You need to login to post a comment.