Calculation of linear distance of "JAVA" two-point latitude and longitude

Source: Internet
Author: User
Tags cos pow sin

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.