I would like to find compass head from two geo location. I am writing C application. I write a function like,
double CalculateHeading(double lat1, double long1, double lat2, double long2)
{
double Latitude = lat1 - lat2;
double Longitude = long1 - long2;
double angle = 0;
printf("\n#################################\n");
printf("Lat1: %lf Lat2: %lf\n", lat1, lat2);
printf("Long1: %lf Long2: %lf\n", long1, long2);
printf("Lat Diff: %lf\n", Latitude);
printf("Long Diff: %lf\n", Longitude);
angle = atan2(Longitude, Latitude);
angle = radiansToDegrees(angle);
printf("Angle: %lf\n", angle);
printf("#################################\n");
return angle;
}
But this not works on the same location. Here I am passing lat1, long1, lat2, long2 in degree. Can anyone help me out here?
Updated Code from http://www.movable-type.co.uk/scripts/latlong.html which is working,
#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
double CalculateHeading(double lat1, double lon1, double lat2, double lon2)
{
double lat1_rad = degreesToRadians(lat1);
double lat2_rad = degreesToRadians(lat2);
double diffLon = lon2-lon1;
double dLon = degreesToRadians(diffLon);
double y = sin(dLon) * cos(lat2_rad);
double x = cos(lat1_rad) * sin(lat2_rad) - sin(lat1_rad) * cos(lat2_rad) * cos(dLon);
double angle = 0;
angle = atan2(y,x);
printf("Angle: %lf\n", radiansToDegrees(fmod(angle + 360.0, 360.0)));
return angle;
}