Python calculates distances based on latitude and longitude examples

Source: Internet
Author: User
Tags cos
Copy the Code code as follows:


/**
* Calculate distance between two points
* @param _lat1-start Latitude
* @param _lon1-start Longitude
* @param _lat2-end Latitude
* @param _lon2-end Longitude
* @return km (rounded)
*/
public static double Getdistance (double _lat1,double _lon1, double _lat2,double _lon2) {
Double LAT1 = (math.pi/180) *_lat1;
Double lat2 = (math.pi/180) *_lat2;

Double lon1 = (math.pi/180) *_lon1;
Double lon2 = (math.pi/180) *_lon2;

Earth radius
Double R = 6378.1;

Double d = Math.acos (Math.sin (LAT1) *math.sin (LAT2) +math.cos (LAT1) *math.cos (LAT2) *math.cos (lon2-lon1)) *r;

return new BigDecimal (d). Setscale (4,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}

public static void Main (string[] args) {
System.out.println (Getdistance (45.73990, 126.55893, 45.73876, 126.55037));
}

  • Related Article

    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.