I have seen several discussions of this on the forum but could not fined anything specific to my needs. Here is a B4X sub that will determine the bearing between two GPS coordinates. It's been translated, slightly modified and tested from the original post by @schimanski (many thanks!). It does not require any external libraries.
Original Post:
Original Post:
Calculate bearing to other location
Is there someone who knows, how to calculate the bearing from my own location to another location? I have tried it with an old sub out of basic4ppc, but it doesn't work... sub Kursberechnung (Lat1,Lon1,Lat2,Lon2 As Double) As Double Dim tc1, sn, d, Kurs As Double 'Umrechnung...
www.b4x.com
Calculate the bearing between two GPS coordinates:
Sub GetBearing (lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Int
Dim d, tc1, sn As Double
Dim latAlt, lonAlt, latNeu, lonNeu As Double
'Conversion of coordinates from degrees to radians
'LatAlt and LonAlt Are the old coordinates from which the bearing is to be determined'
latAlt = lat1 * cPI / 180
lonAlt = -lon1 * cPI / 180
latNeu = lat2 * cPI / 180
lonNeu = -lon2 * cPI / 180
'Formula to calculate the distance between the old and the new position'
d = 2 * ASin(Sqrt(Power((Sin((latAlt-latNeu)/2)),2)+ Cos(latAlt)*Cos(latNeu)*Power((Sin((lonAlt-lonNeu)/2)),2)))
'Formula to calculate the bearing from the old position to the new position depending on north In radians'
If Cos(latAlt) < 1e-7 Then
If (latAlt > 0) Then
tc1 = cPI
Else
tc1= 2*cPI
End If
Else
sn = Sin(lonNeu-lonAlt)
If Abs(sn) < 1e-7 Then
If latAlt > latNeu Then tc1 = cPI Else tc1 = 2*cPI
Else If sn < 0 Then
tc1=ACos((Sin(latNeu)-Sin(latAlt)*Cos(d))/(Sin(d)*Cos(latAlt)))
Else
tc1=2*cPI-ACos((Sin(latNeu)-Sin(latAlt)*Cos(d))/(Sin(d)*Cos(latAlt)))
End If
End If
'Recalculate the bearing from radians to degrees'
Dim bearing As Int
bearing = NumberFormat(tc1 * 180 / cPI,1,0)
Return bearing
End Sub
Last edited: