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