我想从两个地理位置找到罗盘头。 我正在编写 C 应用程序。我写了一个类似的函数,
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;
}
但这不适用于同一位置。这里我以度为单位传递 lat1、long1、lat2、long2。 有人可以帮我吗?
更新了代码 http://www.movable-type.co.uk/scripts/latlong.html正在运行,
#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;
}
最佳答案
angle = atan2(y,x);
printf("Angle: %lf\n", radiansToDegrees(fmod(angle + 360.0, 360.0)));
这里你已经得到了角度的弧度值,所以加上360度是无意义的。 可能的更正:
radiansToDegrees(fmod(angle + 2*Pi, 2*Pi))
or
fmod(radiansToDegrees(angle) + 360.0, 360.0)
关于c - 使用地理坐标查找指南针方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45318930/