Posted By

elightbo on 11/03/06


Tagged

latittude longitude distance


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ejosullivan
jeroldan


ASP Calculate distance between two points given latitude / longitude


 / Published in: ASP
 

  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. const pi = 3.14159265358979323846
  31.  
  32. Function distance(lat1, lon1, lat2, lon2, unit)
  33. Dim theta, dist
  34. theta = lon1 - lon2
  35. dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta))
  36. dist = acos(dist)
  37. dist = rad2deg(dist)
  38. distance = dist * 60 * 1.1515
  39. Select Case ucase(unit)
  40. Case "K"
  41. distance = distance * 1.609344
  42. Case "N"
  43. distance = distance * 0.8684
  44. End Select
  45. End Function
  46.  
  47.  
  48. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  49. '::: This function get the arccos function from arctan function :::
  50. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  51. Function acos(rad)
  52. If Abs(rad) <> 1 Then
  53. acos = pi/2 - Atn(rad / Sqr(1 - rad * rad))
  54. ElseIf rad = -1 Then
  55. acos = pi
  56. End If
  57. End function
  58.  
  59.  
  60. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  61. '::: This function converts decimal degrees to radians :::
  62. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  63. Function deg2rad(Deg)
  64. deg2rad = cdbl(Deg * pi / 180)
  65. End Function
  66.  
  67. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  68. '::: This function converts radians to decimal degrees :::
  69. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  70. Function rad2deg(Rad)
  71. rad2deg = cdbl(Rad * 180 / pi)
  72. End Function
  73.  
  74. 'response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "M") & " Miles<br>"
  75. 'response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "K") & " Kilometers<br>"
  76. 'response.write distance(32.9697, -96.80322, 29.46786, -98.53506, "N") & " Nautical Miles<br>"
  77.  
  78. %>

Report this snippet  

You need to login to post a comment.