The calculation formula from Google Maps:
Called by various methods of the math class in Java. Implement the above formula
private static Double Earth_radius = 6378.137;//unit kilometer/** * Angular radian Calculation formula RAD: (). <br/> * * 360 degrees =2ππ=math.pi * * x degrees = x*π/360 radians * * @author Chiwei * @param d * @return * @since JDK 1.6 */private static double Getradian (double degree) {return degree * math.pi/180.0;} /** * Calculates the distance between two points based on latitude and longitude getdistance: (). <br/> * * * @author Chiwei * @param lat1 * latitude of 1 points * @param lng1 * 1 points Longitude * @param lat2 * 2 points of latitude * @ Param lng2 * longitude of 2 points * @return distance per meter * @since JDK 1.6 */public static double getdistance (double lat1, double lng1, Dou Ble lat2, double lng2) {Double radLat1 = Getradian (LAT1);d ouble radLat2 = Getradian (LAT2);d ouble a = radlat1-radlat2;// Two points latitude difference double b = Getradian (lng1)-Getradian (LNG2);//Two points longitude difference 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;return s * 1000;}
Calculation of latitude and longitude distance between Hefei and Hangzhou
public static void Main (String ar[]) {getdistance (31.86, 117.27, 30.26, 120.19);}
330518 m
Verify that it is correct.
Calculation of linear distance of "JAVA" two-point latitude and longitude