计算两个经纬度之间的真是距离----c++

来源:https://www.open-open.com/lib/view/open1430573897802.html

#include 
#define EARTH_RADIUS  6371.0;// 地球半径,单位千米

static double HaverSin(double theta)
{
    double v = sin(theta / 2);
    return v * v;
}

static double ConvertDegreesToRadians(double degrees)
{
    return degrees * M_PI / 180;
}

	/// 
	/// 计算2个经纬度之间的距离
	/// 
	/// 起始点经度
	/// 起始点纬度
	/// 目标点经度
	/// 目标点纬度
	/// 
double LttdLttdToDistance(double from_lon, double from_lat, double to_lon, double to_lat)
{
    // 用 haversine 公式计算球面两点间的距离。
    // 经纬度转换成弧度
    from_lon = ConvertDegreesToRadians(from_lon);
    from_lat = ConvertDegreesToRadians(from_lat);
    to_lon = ConvertDegreesToRadians(to_lon);
    to_lat = ConvertDegreesToRadians(to_lat);
    // 差值
    double vLon = std::abs(from_lon - to_lon);
    double vLat = std::abs(from_lat - to_lat);
    // h is the great circle distance in radians, great circle 就是一个球体上的切面,它的圆心即是球心的一个周长最大的圆。
    double h = HaverSin(vLat) + cos(from_lat) * cos(to_lat) * HaverSin(vLon);
    auto sh = sqrt(h);
    double distance = 2 * (asin(sqrt(h))) * EARTH_RADIUS;

    // 将距离转换为米
    distance *= 1000;

    return distance;
}

你可能感兴趣的:(C++,c++,算法,开发语言,经纬度,距离)