Posted By

brianyang on 10/09/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

luman
romainsauger


Distance Calculator


 / Published in: JavaScript
 

  1. //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. //::: :::
  3. //::: This routine calculates the distance between two points (given the :::
  4. //::: latitude/longitude of those points). It is being used to calculate :::
  5. //::: the distance between two ZIP Codes or Postal Codes using our :::
  6. //::: ZIPCodeWorld(TM) and PostalCodeWorld(TM) products. :::
  7. //::: :::
  8. //::: Definitions: :::
  9. //::: South latitudes are negative, east longitudes are positive :::
  10. //::: :::
  11. //::: Passed to function: :::
  12. //::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
  13. //::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
  14. //::: unit = the unit you desire for results :::
  15. //::: where: 'M' is statute miles :::
  16. //::: 'K' is kilometers (default) :::
  17. //::: 'N' is nautical miles :::
  18. //::: :::
  19. //::: United States ZIP Code/ Canadian Postal Code databases with latitude :::
  20. //::: & longitude are available at http://www.zipcodeworld.com :::
  21. //::: :::
  22. //::: For enquiries, please contact [email protected] :::
  23. //::: :::
  24. //::: Official Web site: http://www.zipcodeworld.com :::
  25. //::: :::
  26. //::: Hexa Software Development Center © All Rights Reserved 2004 :::
  27. //::: :::
  28. //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  29.  
  30. function distance(lat1, lon1, lat2, lon2, unit) {
  31. var radlat1 = Math.PI * lat1/180
  32. var radlat2 = Math.PI * lat2/180
  33. var radlon1 = Math.PI * lon1/180
  34. var radlon2 = Math.PI * lon2/180
  35. var theta = lon1-lon2
  36. var radtheta = Math.PI * theta/180
  37. var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  38. dist = Math.acos(dist)
  39. dist = dist * 180/Math.PI
  40. dist = dist * 60 * 1.1515
  41. if (unit=="K") { dist = dist * 1.609344 }
  42. if (unit=="N") { dist = dist * 0.8684 }
  43. return dist
  44. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: luman on October 9, 2010

Google Map API: http://briancray.com/2009/06/23/calculate-driving-distance-google-maps-api/

You need to login to post a comment.