#include <math.h>
#include <stdlib.h>
const double EARTH_RADIUS = 6378137;
const double MATHPI = 3.141592653;
static double rad(double d)
{
unsigned int degree;
double cent;
degree = (unsigned int)(d/100);
cent = (d-degree*100)/60;
return (degree+cent) * MATHPI / 180.0;
}
static double GetDistanceWithdrift(double lng1,double lat1,double lng2, double lat2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2.0*asin(sqrt(pow(sin(a/2),2)+cos(radLat1)*cos(radLat2)*pow(sin(b/2),2)));
s = s * EARTH_RADIUS;
return s;
}
int main()
{
//经纬度按dddmm.mmmmm格式
//比如12002.91622=========120度02分54.97320秒(0.91622*60)
double jd1=12002.91622;//jd1=rad(jd1);
double wd1=3030.60666;//wd1=rad(wd1);//杭州
double jd2=11623.56897;//jd2=rad(jd2);
double wd2=3954.25364;//wd2=rad(wd2);//北京
double distance=0.00;
//返回的距离单位为m
distance=GetDistanceWithdrift(jd1,wd1,jd2,wd2);
printf("distance = %f\n",distance);
return 0;
}