2013-08-02 68 views
0

这是我使用下式:计算方位误差

double lat1 = [self getRadiansFromDegress:self.LocationLatitude]; 
    double lat2 = [self getRadiansFromDegress:PointOfInterest.latitude]; 

    double x = sin(lonDiff) * cos(lat2); 
    double y = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(longDiff); 

    double bearingRad = atan2(x,y); 
    double bearingDeg = [self getDegreesFromRadians:bearingRad]; 
    NSLog(@"Bearing: %f", bearingDeg); 

对于LAT1 = 30.61

LAT2 = 30.620954

lon1 = -96.342

lon2 = -96.345238

longDiff = -0.003238

根据这个网站:http://www.yourhomenow.com/house/haversine.html我应该得到大约345度,但我的代码返回-86.028010度。任何人都可以看到为什么这是如此不同?

回答

0

atan2你交换了x和y。 正确的顺序是Y,X:

正确

double bearingRad = atan2(y, x); 

参见:

#include <math.h> 
double atan2(double y, double x);