【发布时间】:2011-04-24 23:11:52
【问题描述】:
我正在尝试为 CLLocation 编写一个类别以将方位返回到另一个 CLLocation。
我认为我的公式做错了(精打细算不是我的强项)。返回的轴承始终处于关闭状态。
我一直在查看这个问题并尝试应用被接受为正确答案的更改及其引用的网页:
Calculating bearing between two CLLocationCoordinate2Ds
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);
}
【问题讨论】:
-
为什么要将纬度和经度值从度数转换为弧度? Haversine 函数是否需要这种转换?
-
回答我自己的问题,是的。 Haversine 函数需要该转换,如下所示:movable-type.co.uk/scripts/latlong.html
标签: iphone mapkit core-location cllocation haversine