ios - Calculating bearing between two CLLocationCoordinate2Ds -


A lot of "simple" problems: two CLLocationCoordinate2Ds are given, how can I get effect from first to second (radians)?

This is my implementation:

  - (float) getHeadingForDirectionFromCoordinate: (CLLocationCoordinate2D) Corollation with lock: (CLLocationCoordinate2D) toLoc {float fLat = fromLoc.latitude; Float fLng = fromLoc.longitude; Float tLat = toLoc.latitude; Float tLng = toLoc.longitude; Return atan2 (sin (fLng-tLng) * cos (tLat), cos (flatt) * sin (TLAT) - sin (flat) * cos (TLAT) * cos (flang-tlng)); }  

However, this method is not returning consistently for me, if the effect is due to the answer or close to the south, it seems ok, however, incompatible data in any other direction Comes back, for example:

from 50.405018, 8.437500

(see And)

I have doubled

1936802, and Triple checked that the formula is fine. I have also seen a bunch of values ​​like an example above, something is right, something is wrong, I have played with different modules or with the return value limit, there is no luck too.

Any thoughts? Is there a problem with my code? Maybe I have misunderstood something about the work of mathematics?

Your math is correct with the following exceptions:

  1. Be sure to convert fLat , fLon , tLat , and tLon or kos () < / Em> Divide any sin () by 180.0 and multiply by PI before implementing them.

  2. between tLng and fLng enter the delta between tLng-fLng , and not on the other side. Note that this difference appears twice in expressions.

With those changes, I am getting 1.18660677830947 radians dual exact mathematics and the value in question.


Comments