两点经纬度之间的距离

时间:2021-10-20 19:21:34
#include <stdio.h>
#include <math.h>


#define PI                      3.1415926
#define EARTH_RADIUS            6378.137        //地球近似半径


// 求弧度
double radian(double d)
{
    return d * PI / 180.0;   //角度1? = π / 180
}


//计算距离
double get_distance(double lat1, double lng1, double lat2, double lng2)
{
    double radLat1 = radian(lat1);
    double radLat2 = radian(lat2);
    double a = radLat1 - radLat2;
    double b = radian(lng1) - radian(lng2);
    
    double dst = 2 * asin((sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2) )));
    
    dst = dst * EARTH_RADIUS;
    dst = round(dst * 10000) / 10000;
    return dst;//km

}

int main(void)

{

    double lat1 = 22.25647;

    double lng1 = 113.97854;

    double lat2 = 22.25444;

    double lng2 = 113.97854;

    double dst = get_distance(lat1, lng1, lat2, lng2);//km

}