0

This is the formula I am using:

 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);

For lat1 = 30.61

lat2 = 30.620954

lon1 = -96.342

lon2 = -96.345238

longDiff = -0.003238

According to this website: http://www.yourhomenow.com/house/haversine.html I should be getting about 345 degrees but my code is returning -86.028010 degrees. Can anyone see why this is so different?

user2125844
  • 315
  • 2
  • 3
  • 11

1 Answers1

0

in atan2 you exchanged x and y. The correct order is y,x:

Correct to

double bearingRad = atan2(y, x);

See also:

#include <math.h>
double atan2( double y, double x );
AlexWien
  • 28,470
  • 6
  • 53
  • 83