iOS Question Calculate bearing to other location

schimanski

Well-Known Member
Licensed User
Longtime User
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...

B4X:
sub Kursberechnung (Lat1,Lon1,Lat2,Lon2 As Double) As Double
   
    Dim tc1, sn, d, Kurs As Double 
   
    'Umrechnung der Koordinaten von Grad in Bogemmaß'
    Lat1 = Lat1 * cPI / 180
    Lon1 = -Lon1 * cPI / 180
    Lat2 = Lat2 * cPI / 180
    Lon2 = -Lon2 * cPI / 180
   
    'Formel zur Berechnung der Distanz zwischen der aktuellen Position und dem Ziel'
    d = 2 * ASin(Sqrt((Sin((lat1-lat2)/2))^2 + Cos(lat1)*Cos(lat2)*(Sin((lon1-lon2)/2))^2))
   
    'Formel zur Berechnung des Kurses vom aktuellen Standpunkt zum Ziel in Anhängigkeit vom Norden in Bogenmaß'
    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
   
    'Rückrechnung des Kurses von Bogenmaß in °'
    Kurs = tc1 * 180 / cPI
   
    Return Kurs
   
End Sub

Thanks for help....
 

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried several methods to calculate the bering from one location to another, but the result is always wrong:confused:.
It doesn't makes it easier for me, when I convert the coordinates to UTM. Did someone have a formula to work with?

Thanks for help..
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Convert to UTM with your Geodesic module is no problem and the distances are max. 50 km. But I'm not familar with UTM, so I don't know, how to calculate the bearing to....
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Much thanks, Erel. I have tested it and the distance is correct. But with several coordinates, I have a drift from 30° between the solution of bearingto in b4a and your formel in b4i. I think, that the bearing in Bearing = ATan2D(y2 - y1, x2 - x1) is in degrees (-180 to +180°)???
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
o.k, thanks, but it's always the same problem, for example:
Location1 to Location2 in B4a with Bearingto=240°
Location1 to Location2 in B4i with ATan2D(y2 - y1, x2 - x1)=209°

:confused:
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Yes, I have checked my own calculated results with your Coordinates Converter of the app store...
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks for your efforts, Erel. But I made it now with the old method. The following sub works correct, I have tested it with several different locations. Perhaps, there is someone, who need it:

B4X:
Private Sub Application_Start (Nav As NavigationController)
    .....
    Dim Location1, Location2 As Location 
    Location1.Initialize2("50.289459", "6.791009")
    Location2.Initialize2("51.346536", "6.343463")
    Msgbox(Kursberechnung(Location1, Location2), "Kurs")
End Sub

Sub Kursberechnung(LocationAlt As Location, LocationNeu As Location) As Int

    Dim d, tc1, sn As Double
    Dim latAlt, lonAlt, latNeu, lonNeu As Double
   
   
    'Umrechnung der Koordinaten von Grad In Bogemmaß'
    'LatAlt und LonAlt Sind die alten Koordinaten, aus deren Sicht der Kurs bestimmt werden soll'
    latAlt = LocationAlt.Latitude * cPI / 180       
    lonAlt = -LocationAlt.Longitude * cPI / 180
    latNeu = LocationNeu.Latitude * cPI / 180
    lonNeu = -LocationNeu.Longitude * cPI / 180
   
    'Formel zur Berechnung der Distanz zwischen der alten und der neuen Position'
    d = 2 * ASin(Sqrt(Power((Sin((latAlt-latNeu)/2)),2)+ Cos(latAlt)*Cos(latNeu)*Power((Sin((lonAlt-lonNeu)/2)),2)))
    
    'Formel zur Berechnung des Kurses von der alten Position zur neuen Position in Abhängigkeit vom Norden In Bogenmaß'
    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
   
    'Rückrechnung des Kurses von Bogenmaß in Grad'
    Dim Kurs As Int
    Kurs = NumberFormat(tc1 * 180 / cPI,1,0)
   
    Return Kurs
End Sub
 
Upvote 0
Top