距离计算有两种公式,Vincenty和Haversine,前者经度很高,可以达到0.5毫米,但是速度慢;后者速度快,但是经度差点,但是也不含糊,实际中很常用,只要是精度要求不是特别高的例如航空、导弹等的领域,还是很实用的。例如在只能网联汽车领域,后者应用很广泛。
原文见:
计算球面两点间距离实现Vincenty+Haversine
这里将上述链接中Vincenty公式的源码copy过来:
double toRadians(double a)
{
return a*(PI/180)
}
//return the distance of two point, unit is meter
double distance_2(double latitude1, double longitude1, double latitude2, double longitude2) {
// R is the radius of the earth in meters
double R = 6371004; //EARTH_RADIUS
double deltaLatitude = toRadians(latitude2-latitude1);
double deltaLongitude = toRadians(longitude2-longitude1);
latitude1 =toRadians(latitude1);
latitude2 =toRadians(latitude2);
double a = pow(sin(deltaLatitude/2), 2)+ cos(latitude1)* cos(latitude2)* pow(sin(deltaLongitude/2), 2);
double c = 2 * atan2(sqrt(a),sqrt(1-a));
double d = R * c;
return d;
}
更详细的讲解见:根据两点的经纬度求方位角和距离
这里有详细的公式噢~
文章中的提到的球面正弦定律,没有详细展开,这里补充下:
球面三角正弦定理