Sub GetDistance(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
' lat1 and long1 are the previous position, lat2 and long2 are the current position
Dim rlat1, rlong1, rlat2, rlong2, p1, p2, p3, ret As Double
rlat1 = DEGREES_TO_RADIANS * lat1
rlong1 = DEGREES_TO_RADIANS * long1
rlat2 = DEGREES_TO_RADIANS * lat2
rlong2 = DEGREES_TO_RADIANS * long2
p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2) ' funcky code to measure distance - works very well so far
p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2) ' dont ask me how it works - but it does!!!
p3 = Sin(rlat1) * Sin(rlat2)
ret = p1 + p2 + p3
If ret >= 1 Then ' greater than or equal to - otherwise NAN (not a number ) error
Return 0
Else
ret = ACos(ret)
ret = (ret * EARTH_RADIUS)
'ToastMessageShow(" PVals: "&p1&" - "&p2&" - "&p3&CRLF&" Ret1: "&ret1&" Ret2: "&ret2&" Retp: "&retp&" Res: "&ret ,True)
Return ret
End If
End Sub