球面上两个经纬度点之间的距离公式
private static final double EARTH_RADIUS = 6378137.0; //
private static double getRad(Double d) {
return d * Math.PI / 180.0;
}
/**
* 113.5227398,38.38114998;
113.5320502,38.40294996;
*/
@Test
public void testJuLi() {
/**石家庄北站
* 114.471834,38.072762
*石太公园
*114.449556,38.065035
*测试结果2133.4924 实际距离应该是2.1公里
*/
double a = 114.471834;
double b = 38.072762;
double c = 114.449556;
double d = 38.065035;
double distance = EarthTwoPointsDistance(a,b,c,d);
//2559.1132
System.out.println(distance);
}
public double EarthTwoPointsDistance(double lng1, double lat1, double lng2, double lat2) {
double radLat1 = getRad(lat1);
double radLat2 = getRad(lat2);
double a = radLat1 - radLat2;
double b = getRad(lng1) - getRad(lng2);
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 * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000.0;
return s;
}