iOS Question Calculate bearing to other location

Discussion in 'iOS Questions' started by schimanski, Nov 5, 2014.

  1. schimanski

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

    Code:
    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 > 0Then
                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....
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can convert the coordinates to UTM with the Geodesic module (see the Coordinates Converter example).

    UTM coordinates are simpler to work with as it is a standard cartesian grid.
     
  3. schimanski

    schimanski Well-Known Member Licensed 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..
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Were you able to correctly convert them to UTM? UTM is much simpler to work with (assuming that the distances are not huge).
     
  5. schimanski

    schimanski Well-Known Member Licensed 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....
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    UTM is a simple grid (unlike Lat / Lon). The unit is meters.

    Distance = Sqrt(Power(x2 - x1, 2) + Power(y2 - y1, 2))

    Bearing = ATan2D(y2 - y1, x2 - x1)
     
  7. schimanski

    schimanski Well-Known Member Licensed 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°)???
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can convert it to 0 - 360 with:
    (degrees + 360) Mod 360
     
  9. schimanski

    schimanski Well-Known Member Licensed 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:
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Which one is correct?
     
  11. schimanski

    schimanski Well-Known Member Licensed User

    Bearingto from b4a...
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Check the UTM coordinates. Are they correct?
     
  13. schimanski

    schimanski Well-Known Member Licensed User

    Yes, I have checked my own calculated results with your Coordinates Converter of the app store...
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    You need to check them with a different tool as both are based on the same code.

    Assuming that they are correct then please post the UTM coordinates with the relevant code.
     
  15. schimanski

    schimanski Well-Known Member Licensed 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:

    Code:
    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 LocationAs 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 > 0Then
                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
     
    MikeH, MotoMusher, Albi and 3 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice