c语言 经纬度计算距离

//=========================================================================
#include <stdio.h>
#include <math.h>
#define PI                      3.1415926
#define EARTH_RADIUS            6378137        //地球近似半径


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

//unit meter
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;
	if(dst<0)
		{
		dst=-dst;
		}
    return dst;
}
//=========================================================================
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页