I am trying to calculate the distance between 2 points on earth...hum, probably 2 airports
I have read the posts related to Haversine formula and visited 1/2 million web sites. I am using VB5 (yeah, old class). I adapted the formulas I found into code and though it all 'looks good' it doesn't work all that well. If anyone can give me a hint on what I am doing wrong I would appreciate.
References:
Haversine formula: http://www.ig.utexas.edu/outreach/googleearth/latlong.html
Atan2:http://en.wikibooks.org/wiki/Visual_Basic/Simple_Arithmetic
Data:
KGSO: N36* 06.079552',W079 56.467407'
KMYR: N33* 40.785000',W078 55.700000'
Calculated distance on utexas.edu is 284.7 km
Calculated distance on google is 315.4 km
My calculated distance is 122.6 km
Here is the code:
References:
Haversine formula: http://www.ig.utexas.edu/outreach/googleearth/latlong.html
Atan2:http://en.wikibooks.org/wiki/Visual_Basic/Simple_Arithmetic
Data:
KGSO: N36* 06.079552',W079 56.467407'
KMYR: N33* 40.785000',W078 55.700000'
Calculated distance on utexas.edu is 284.7 km
Calculated distance on google is 315.4 km
My calculated distance is 122.6 km
Here is the code:
Code:
Private Const Pi As Double = 3.14159265358979
Public Function GetDistance(ByVal sValue1 As String, ByVal sValue2 As String) As Double
' sValue1 format: N36* 8.02',W80* 13.31
Dim R As Double
Dim dLat As Double
Dim dLon As Double
Dim dLat1 As Double
Dim dLon1 As Double
Dim dLat2 As Double
Dim dLon2 As Double
Dim a As Double
Dim c As Double
CoordToDegree sValue1, dLat1, dLon1
CoordToDegree sValue2, dLat2, dLon2
' R = 3437.74677 '(nautical miles)
R = 6378.7 '(kilometers)
' R = 3963 '(statute miles)
dLat1 = DegToRad(dLat1)
dLon1 = DegToRad(dLon1)
dLat2 = DegToRad(dLat2)
dLon2 = DegToRad(dLon2)
dLat = dLat2 - dLat1
dLon = dLon2 - dLon1
a = (Sin(dLat / 2) * Sin(dLat / 2)) + ((Cos(dLat1) * Cos(dLat2)) * (Sin(dLon / 2) * Sin(dLon / 2)))
c = 2 * Atan2(Sqr(a), Sqr(1 - a))
GetDistance = R * c
End Function
Public Sub CoordToDegree(ByVal sValue As String, ByRef dLon As Double, ByRef dLat As Double)
' [url]http://www.chocolatesoftware.com/forum/index.php?topic=839.0[/url]
Dim pos As Integer
Dim iDegree As Integer
Dim bMinus As Boolean
Dim cnt As Integer
Dim tmpStr As String
tmpStr = sValue
pos = InStr(1, tmpStr, ",")
tmpStr = Right(tmpStr, Len(tmpStr) - pos)
redo:
cnt = cnt + 1
If UCase(Left(tmpStr, 1)) = "S" Or UCase(Left(tmpStr, 1)) = "W" Then bMinus = True
tmpStr = Right(tmpStr, Len(tmpStr) - 1)
pos = InStr(1, tmpStr, "* ")
iDegree = Left(tmpStr, pos - 1)
tmpStr = Right(tmpStr, Len(tmpStr) - (pos + 1))
pos = InStr(1, tmpStr, "'")
If cnt = 1 Then
dLon = iDegree + (Left(tmpStr, pos - 1)) / 60
If bMinus Then dLon = -dLon
Else
dLat = iDegree + (Left(tmpStr, pos - 1)) / 60
If bMinus Then dLat = -dLat
End If
pos = InStr(1, sValue, ",")
tmpStr = Left(sValue, pos - 1)
If cnt = 1 Then GoTo redo
End Sub
Public Function DegToRad(ByVal dvalue As Double) As Double
DegToRad = dvalue * (3.14159265358979 / 180)
End Function

