A点经纬度为(lat1,lng1),B点经纬度坐标为(lat2,lng2)。以A点为参照,计算B点的方向与距离
public static Object[] getAngleAndDistance(Double lat1,Double lng1,Double lat2,Double lng2){
Object[] obj =new Object[2];
double a = Math.toRadians(90-lat2);
double b = Math.toRadians(90-lat1);
double ab = Math.toRadians(lng2-lng1);
double cosc = Math.cos(a)*Math.cos(b)+Math.sin(a)*Math.sin(b)*Math.cos(ab);
if (cosc < -1.0) cosc = -1.0;
if (cosc >1.0) cosc =1.0;
double c = Math.acos(cosc);
double sinA = (Math.sin(a)*Math.sin(ab))/Math.sin(c);
if (sinA < -1.0) sinA = -1.0;
if (sinA >1.0) sinA =1.0;
double A = Math.asin(sinA);
double Aangle = Math.toDegrees(A);
if (lng2 == lng1){
if (lat2 > lat1){
obj[0] ="正北";
}else if (lat2
obj[0] ="正南";
}else{
obj[0] ="正中";
}
}else if (lat2 == lat1){
if (lng2 > lng1){
obj[0] ="正东";
}else if (lng2 < lng1){
obj[0] ="正西";
}else{
obj[0] ="正中";
}
}else if (lng2 > lng1 && lat2 > lat1){//B相对于A来说位于第一象限
}else if (lng2 < lng1 && lat2 > lat1){//第二象限
Aangle =360+Aangle;
}else{//第三,四象限
Aangle =180-Aangle;
}
if (obj[0] ==null){
if (Aangle <=22.5 || Aangle >337.5){
obj[0] ="正北";
}else if (Aangle >22.5 && Aangle <=67.5){
obj[0] ="东北";
}else if (Aangle >67.5 && Aangle <=112.5){
obj[0] ="正东";
}else if (Aangle >112.5 && Aangle <=157.5){
obj[0] ="东南";
}else if (Aangle >157.5 && Aangle <=202.5){
obj[0] ="正南";
}else if (Aangle >202.5 && Aangle <=247.5){
obj[0] ="西南";
}else if (Aangle >247.5 && Aangle <=247.5){
obj[0] ="正西";
}else if (Aangle >247.5 && Aangle <=337.5){
obj[0] ="西北";
}
}
obj[1] =getDistance(lat1,lng1,lat2,lng2);
return obj;
}
getDistance:
Double radLat1 =lat1* Math.PI/180.0;
Double radLat2 =lat2* Math.PI/180.0;
Double a = radLat1 - radLat2;
Double b =lng1* Math.PI/180.0 -lng2* Math.PI/180.0;
Double s =2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s *6378.137;
s = Math.round(s *10000)*1.0/10000;
原理参考:原理地址