1. 程式人生 > >c++根據兩點經緯度座標獲取長度

c++根據兩點經緯度座標獲取長度

#define PI 3.141592657
#define EARTH_RADIUS  6378137
static inline double  rad( double degree )
{
    return  PI * degree / 180.0;
}
static inline double haverSin(double x)
{
    double v = sin(x / 2.0);
    return v * v;
}
//計算距離(單位 : m)
static double getDistance(double lon1, double lat1, double lon2, double lat2)
{
    double radlon1 = rad(lon1);
    double radlat1 = rad(lat1);
    double radlon2 = rad(lon2);
    double radlat2 = rad(lat2);
    
    double a = fabs(radlat1 - radlat2);
    double b = fabs(radlon1 - radlon2);
    
    double h = haverSin(b) + cos(lat1)*cos(lat2)*haverSin(a);
    double distance = 2 * EARTH_RADIUS * asin(sqrt(h));
    return  distance;
}