评论

收藏

[C++] 计算两点经纬度点的之间的距离和方向角,正北为0度

编程语言 编程语言 发布于:2021-07-11 10:55 | 阅读数:438 | 评论:0

double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
   double angle = PI/180.0;
  return (2*asin(sqrt(pow(sin((lat1-lat2)*angle/2),2) + cos(lat1*angle)*cos(lat2*angle)*pow(sin((lng1-lng2)*angle/2),2))))*6378.137;//地球半经:6378.137Km
}
 
int GeoAngle(double lat1, double lon1, double lat2, double lon2)
{
  double angle = 3.14159265358979324/180;
  double dY = cos(lon2*angle) * sin(lat2*angle - lat1*angle);
  double dX = cos(lon1*angle) * sin(lon2*angle) - sin(lon1*angle) * cos(lon2*angle) * cos(lat2*angle - lat1*angle);
  int dirE0 = 180 * atan2(dY, dX) / 3.14159265358979324;
  if (dirE0 < 0)
  {
    dirE0 = dirE0 + 360; //0~360
  }
  int dirN0 = (dirE0 + 90) % 360;
  return (dirN0);
}
  
关注下面的标签,发现更多相似文章