Java calculates distances based on latitude and longitude

Source: Internet
Author: User

In the actual map project will encounter the calculation of distance, here to write, later reference:

  

/**
* @author Author: Zhaoliang
* @version: v1.0
* Date of creation: June 26, 2018 3:32:30
* Class Description: Calculate distance according to latitude and longitude
*/
public class Distanceutil {

private static Double Earth_radius = 6378.137; Earth radius
/**
* Google Maps calculates the distance of two coordinate points
* Longitude @param latitude page passed over
* @param the latitude that longitude page passed over
* @param the longitude of parking lot in GAODE_LNG database
* @param the latitude of parking lot in Gaode_lat database
* @return distance (km)
*/
public static double getdistance (double latitude, double longitude, double gaodelng, double Gaodelat) {
Double RADLAT1 = Math.toradians (longitude);
Double radLat2 = Math.toradians (Gaodelat);
Double A = RADLAT1-RADLAT2;
Double b = Math.toradians (latitude)-Math.toradians (GAODELNG);
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;
}

public static void Main (string[] args) {
Long B = System.currenttimemillis ();
for (int i=0; i<1000000; i++) {
Getdistance (116.403933,39.914147, 116.403237,39.927919);
}
SYSTEM.OUT.PRINTLN ("Time Consuming:" + (System.currenttimemillis ()-B) + "millisecond"); Time: 461 ms
Double dist = getdistance (116.403933,39.914147, 116.403237,39.927919);
System.out.println ("Two points apart:" + dist + "km"); Two points apart: 1.0 km

}
}

Java calculates distances based on 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.