' Function LatLongDist() - Calulates the arc distance between two Lat, Long where...
' lat and long are in degrees in the form degrees=ddd + mm/60 + sss/3600
' i.e. 29 44' 00" = 29+44/60+0/3600 = 29.7333333
' first point is (lat1, long1)
' second point is (lat2, long2)
' EarthRadius is the radius of the earth, use KM for calculations in KM, miles for calculations in miles, and NM for calculations in NM
' EarthRadius = 3440 NM
' EarthRadius = 6371 KM (6378.137 KM at the Equator, 6356.752 at poles, use 6371 [volumetric mean radius) for most calculations)
' EarthRadius = 3959 Miles
Function LatLongDist(lat1, long1, lat2, long2, EarthRadius)
Dim cos1, cos2, sin1, sin2, lat1rad, lat2rad, lond1rad, long2rad, _
straightLineDist, arcDist, x1, y1, z1, x2, y2, z2, radius As Double
radius = EarthRadius
'
'Convert Lat Long to Radians
'
lat1rad = lat1 / 180 * 3.14159
lat2rad = lat2 / 180 * 3.14159
long1rad = long1 / 180 * 3.14159
long2rad = long2 / 180 * 3.14159
'
' Convert (R,T,r) (Rho, Theta, radius) Polar coordinates into standard (x,y,z) coordinates
'
x1 = Sin(lat1rad) * Cos(long1rad) * radius
y1 = Sin(lat1rad) * Sin(long1rad) * radius
z1 = Cos(lat1rad) * radius
x2 = Sin(lat2rad) * Cos(long2rad) * radius
y2 = Sin(lat2rad) * Sin(long2rad) * radius
z2 = Cos(lat2rad) * radius
'
' Calc straight line distance between 2 points in 3D space
'
straightLineDist = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2)
'
' Use law of cosines to calculate arc distance
'
arcDist = WorksheetFunction.Acos(1 - (straightLineDist ^ 2 / (2 * radius ^ 2))) * radius
'
'return arc distance
'
LatLongDist = arcDist
'
' comment above and uncomment below to return straight line distance
'
'LatLongDist = straightLineDist
'
End Function