Is this a valid way to calculate an angle (in radians) from one CLLocation to another?
-(float)angleFromLocation:(CLLocationCoordinate2D)start toLocation:(CLLocationCoordinate2D)end {
float deltaX = start.latitude - end.latitude;
float deltaY = start.longitude - end.longitude;
float ang = atan2(deltaY, deltaX);
return ang;}
Please advise!
Any help will be much appreciated.
I used a variant of this question and answer and it works well:
Swift 4 version:
The best method I found for this computation was to use the Spherical Law of Cosines. There is a C function to do this available here on github called headingInDegrees. It takes two lat/long pairs and returns heading:
Since a CLLocationCoordinate2d contains latitude and longitude, it is easy to pass those two fields to this function and get the heading back.