C语言根据经纬度计算距离

#include


#define EARTH_RADIUS 6378.137//地球半径
#define PI 3.14159265358979323846 //圆周率



//角度转化为弧度
static double rad(double d)
{
   return d * PI / 180.0;
}


double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);


double s = 2 * asin(sqrt(pow(sin(a/2),2) +
cos(radLat1)*cos(radLat2)*pow(sin(b/2),2)));
s = s * EARTH_RADIUS;
//s = round(s * 10000) / 10000;
s = llroundl(s*10000) / 10000.0;
//s = double b = ( ( int )( a * 1000000 + 0.5 ) ) / 1000000.0;//round(11111111.21);
//s = s/10000;

return s;
}

int main()

{

double dist = GetDistance(0,1, 0, 1.001 );
printf("1.dist=%f\n", dist );

dist = GetDistance(90-24.0,0, 90+24.01, 0 );
printf("2.dist=%f\n", dist );

dist = GetDistance(24.0,5, -24, -1 );
printf("3.dist=%f\n", dist );

dist = GetDistance(24,5, 24, -1 );
printf("4.dist=%f\n", dist );

dist = GetDistance(48,5, 24, -1 );
printf("5.dist=%f\n", dist );

dist = GetDistance(-50.0,120, -10, 120 );
printf("6.dist=%f\n", dist );

while(1);

return 0;

}

你可能感兴趣的:(单片机)