c - 使用地理坐标查找指南针方向

标签 c geolocation geometry compass-geolocation

我想从两个地理位置找到罗盘头。 我正在编写 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/

相关文章:

vector - openGL 中的一颗行星 : vector data or texture mapping?

java - 如何在 JavaFX (fxml) 中制作一个环

c - 在 C 中以分钟为单位测量时间

c - 在 g_signal_connect 中将整数作为 gpointer 传递

javascript - 我的笔记本电脑上的 Chrome 为何能够提供如此精确的地理定位?

geolocation - 计算传单中两点之间的距离

C - 如果字 x 中的任何偶数位设置为 1,则返回 1

c - 为什么 Bison 只打印输入?

android - 从坐标获取国家?

ios - SceneKit 自定义几何图形 : I know multi position(SCNVector). 如何创建自定义几何图形