Calculate the distance between them by two longitude and latitude points

Source: Internet
Author: User

I did not know this for a long time. I forgot it. I saw it in a book before. But what I found on the Internet was actually a spherical distance algorithm.

 

Lat is the latitude, and lung is the longitude
A = Lat1-Lat2 is the difference between two points of latitude B = Lung1-Lung2 is the difference between two points of longitude;
6378.137 (KM) is the earth's radius --@

Well, the result is also km ......

In addition, google's api has actually been implemented
Java code
// Calculate the two-point distance
Private final double EARTH_RADIUS = 6378137.0;
Private double gps2m (double lat_a, double lng_a, double lat_ B, double lng_ B ){
Double radLat1 = (lat_a * Math. PI/180.0 );
Double radLat2 = (lat_ B * Math. PI/180.0 );
Double a = radLat1-radLat2;
Double B = (lng_a-lng_ B) * 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 * EARTH_RADIUS;
S = Math. Round (s * 10000)/10000;
Return s;
}

Author "Notepad ......"

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.