我正在尝试为 CLLocation 编写一个类别,以将轴承返回到另一个 CLLocation。
我相信我在公式上做错了(计算不是我的强项)。返回的轴承始终处于关闭状态。
我一直在查看这个问题并尝试应用被接受为正确答案的更改及其引用的网页:
计算两个 CLLocationCoordinate2D 之间的方位
http://www.movable-type.co.uk/scripts/latlong.html
感谢您的任何指示。我已经尝试整合来自其他问题的反馈,但我仍然没有得到任何东西。
谢谢
这是我的类别-
----- CLLocation+Bearing.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation;
-(NSString *) compassOrdinalToLocation:(CLLocation *) nwEndPoint;
@end
---------CLLocation+Bearing.m
#import "CLLocation+Bearing.h"
double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};
@implementation CLLocation (Bearing)
-(double) bearingToLocation:(CLLocation *) destinationLocation {
double lat1 = DegreesToRadians(self.coordinate.latitude);
double lon1 = DegreesToRadians(self.coordinate.longitude);
double lat2 = DegreesToRadians(destinationLocation.coordinate.latitude);
double lon2 = DegreesToRadians(destinationLocation.coordinate.longitude);
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double radiansBearing = atan2(y, x);
return RadiansToDegrees(radiansBearing);
}