AccessBlog.net

News, links, downloads, tips and tricks on Microsoft Access and related

About Me Search
Alex
Name:Alex Dybenko

Location:Moscow, Russia

Tuesday, September 06, 2005

Use Latitude And Longitude To Find Distance Between Two Points

This post in Microsoft Support newsgroups I found useful (not sure about name of poster, sorry...):


Const PI As Double = 3.14159265358979
Const Circumference As Double = 40123.648 'kilometers
Const MilesPerKilometer As Double = 0.6214
Public Function Distance(ByVal Latitude1 As Double, _
ByVal Longitude1 As Double, _
ByVal Latitude2 As Double, _
ByVal Longitude2 As Double, _
Optional Miles As Boolean) As Double

Dim CosArc As Double
Dim Arc As Double
Latitude1 = Radians(Latitude1)
Longitude1 = Radians(Longitude1)
Latitude2 = Radians(Latitude2)
Longitude2 = Radians(Longitude2)
CosArc = (Sin(Latitude1) * Sin(Latitude2)) + _
(Cos(Latitude1) * Cos(Latitude2) * Cos(Longitude1 - Longitude2))
Arc = Degrees(Atn(-CosArc / Sqr(-CosArc * CosArc + 1)) + 2 * Atn(1))
Distance = Arc / 360 * Circumference
If Miles = True Then Distance = Distance * MilesPerKilometer
End Function
Private Function Radians(ByVal Degrees As Double) As Double
Radians = PI * Degrees / 180
End Function
Private Function Degrees(ByVal Radians As Double) As Double
Degrees = Radians / PI * 180
End Function




Testing on New Delhi (28 37 / -77 13) and
San Francisco ( 37 48 / 122 27)
we get 12380.0276235478 kilometers.
National Graphic Family Reference Atlas gives 12380 km ...
so you may have to walk 25 extra yards.

Caution: the second number in latitude and longitude designations are minutes and must be divided by 60 before being passed to the function as in Debug.Print Distance(28 + 37 / 60, -77 - 13 / 60, 37 + 48 / 60, 122 + 27 / 60) (for the two cities bove).

Signs must be adjusted for north and south latitude and east and west longitude ...
one positive and the other negative If one gets to seconds then the numerator will be 3600.

1 Comments:

Anonymous Anonymous said...

Thanks, Alex.

I'm using this function on a project, and it is a big help.

John C.
New Orleans, LA

5:01 PM  

Post a Comment

Links to this post:

Create a Link

<< Home