I need to find the angle between the north and a point in a map (21.4167, -39.8167).
here is my code
double MECCA_LONGITUDE = 21.4167;
double MECCA_LATITUDE = -39.8167;
-(void) getAngle :(float) phoneAngle: (float) lon:(float) lat
{
float ad = atan (sin (lon - MECCA_LATITUDE) / ( (cos (lat)) * (tan (MECCA_LATITUDE)) ) - ( (sin (lat)) * (cos (lon - MECCA_LONGITUDE)) ));
NSLog(@"###### %f", CC_RADIANS_TO_DEGREES(ad));
}
I’m still not getting the correct answer. Any tips would be appreciated. Thanks.
Your principle problem is that
sin()andcos()take their arguments in radians, and you are passing degrees (you also have the longitude and latitude of Mecca interchanged).Try: