'This sub calculates the distance and course between two Lat/Lon coordinates.
'The formulas are based on this site: http://williams.best.vwh.net/avform.htm (Ed Williams)
Sub DistanceCourse
Dim lat1 As Double = Target.Latitude
Dim lon1 As Double = Target.Longitude
Dim lat2 As Double = MeI.Latitude 'my position
Dim lon2 As Double = MeI.Longitude 'my position
Try
lat1 = lat1 * cPI / 180
lon1 = -lon1 * cPI / 180
lat2 = lat2 * cPI / 180
lon2 = -lon2 * cPI / 180
Dim d As Double = 2 * ASin(Sqrt(Power(Sin((lat1-lat2)/2),2) + Cos(lat1)*Cos(lat2)*Power(Sin((lon1-lon2)/2),2)))
Dim Dist As Double = d * 180 * 60 * 1852/cPI
Dim tc1 As Double
If Cos(lat1) < 1e-7 Then
If (lat1 > 0) Then
tc1 = cPI
Else
tc1= 2*cPI
End If
Else
sn = Sin(lon2-lon1)
If Abs(sn) < 1e-7 Then
If lat1 > lat2 Then tc1 = cPI Else tc1 = 2*cPI
Else If sn < 0 Then
tc1=ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1)))
Else
tc1=2*cPI-ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1)))
End If
End If
Course = tc1 * 180 / cPI
'calculating decimal X,Y
'x=Dist* Cos(tc1-cPI/2)
'y=Dist* Sin(tc1-cPI/2)
Return
Catch
Dist = 0
Course = 0
End Try
End Sub