  # iOS Question Calculate bearing to other location

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

1. 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 > 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....

2. 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. I have tried several methods to calculate the bering from one location to another, but the result is always wrong .
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. Were you able to correctly convert them to UTM? UTM is much simpler to work with (assuming that the distances are not huge).

5. 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. 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. 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. You can convert it to 0 - 360 with:
(degrees + 360) Mod 360

9. 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° 10. Which one is correct?

11. Bearingto from b4a...

12. Check the UTM coordinates. Are they correct?

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

14. 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. 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 SubSub 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 KursEnd Sub`

MikeH, MotoMusher, Albi and 3 others like this.