如何使用GPS坐标计算2个地方之间的距离?
答案 0 :(得分:10)
您必须使用hasrsine公式: Haversine公式:
R =地球半径(平均半径= 6,371km)
Δlat= lat2- lat1
Δlong= long2-long1
a =sin²(Δlat/ 2)+ cos(lat1).cos(lat2).sin²(Δlong/ 2)
c = 2.atan2(√a,√(1-a))
d = R.c
其中d是距离(您的解决方案),所有角度必须以弧度
查找hasrsine库,并在C:
中#include <math.h>
#include "haversine.h"
#define d2r (M_PI / 180.0)
//calculate haversine distance for linear distance
double haversine_km(double lat1, double long1, double lat2, double long2)
{
double dlong = (long2 - long1) * d2r;
double dlat = (lat2 - lat1) * d2r;
double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = 6367 * c;
return d;
}
double haversine_mi(double lat1, double long1, double lat2, double long2)
{
double dlong = (long2 - long1) * d2r;
double dlat = (lat2 - lat1) * d2r;
double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = 3956 * c;
return d;
}
答案 1 :(得分:3)
http://en.wikipedia.org/wiki/Great-circle_distance
(因为地球的球形,毕达哥拉斯定理是不够的)