double R = 6371004; ////地球半径
double dd = M_PI/180;
double x1 = latitude1 * dd;
double x2 = latitude2 * dd;
double y1 = longitude1 * dd;
double y2 = longitude2 * dd;
double distance = (2*R*asin(sqrt(2-2*cos(x1)*cos(x2)*cos(y1-y2) - 2*sin(x1)*sin(x2))/2));
// return distance*1000; //返回 km
// return distance; //返回 m
另一种
double EARTH_RADIUS = 6371004; ////地球半径
double dd = M_PI/180;
double redLat1 = latitude1 * dd;
double redLat2 = latitude2 * dd;
double a = latitude1 * dd - latitude2 * dd;
double b = longitude1 * dd - longitude2 * dd;
double s = 2 * asin(sqrt(pow(sin(a/2), 2) + cos(redLat1)*cos(redLat2)*pow(sin(b/2), 2)));
s = s * EARTH_RADIUS; //返回单位:m