根据经纬度,计算地球上两点之间的距离:
public static double getDistance(double lngA,double latA,double lngB,double latB){
Double r = 6378.137; //半径
//求A点的空间坐标
double xA = Math.cos(latA) * Math.cos(lngA);
double yA = Math.cos(latA) * Math.sin(lngA);
double zA = Math.sin(latA);
//求B点的空间坐标
double xB = Math.cos(latB) * Math.cos(lngB);
double yB = Math.cos(latB) * Math.sin(lngB);
double zB = Math.sin(latB);
//O点是圆心
//求距离
double OA = Math.sqrt(Math.pow(xA, 2) + Math.pow(yA, 2) + Math.pow(zA, 2));
double OB = Math.sqrt(Math.pow(xB, 2) + Math.pow(yB, 2) + Math.pow(zB, 2));
double AB = Math.sqrt(Math.pow(xA - xB, 2) + Math.pow(yA - yB, 2) + Math.pow(zA - zB, 2));
//求OA、OB之间的夹角弧度
double rad = Math.acos((Math.pow(OA, 2) + Math.pow(OB, 2) - Math.pow(AB, 2)) / (2 * OA * OB)) / 180 * Math.PI;
return rad * r;
}
单位:千米。
原文地址:http://blog.csdn.net/colorall/article/details/6119126