Java implementation to calculate the distance between the two longitude and latitude points, directly on the code, specific explanations please refer to the note
Copy Code code as follows:
Package com.jttx.poi.utils;
Import Com.jttx.poi.entity.Point;
/**
* Created by Louis on 2014/9/2.
*/
public class Geoutils {
/**
* Calculate the distance between the two latitude and longitude points (unit: m)
* @param lng1 Longitude
* @param LAT1 Latitude
* @param lng2
* @param lat2
* @return
*/
public static double Getdistance (double lng1,double lat1,double lng2,double lat2) {
Double RADLAT1 = Math.toradians (LAT1);
Double radLat2 = Math.toradians (LAT2);
Double A = RADLAT1-RADLAT2;
Double b = Math.toradians (lng1)-Math.toradians (LNG2);
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 * 6378137.0;//take the Earth's long radius in the reference ellipsoid of the WGS84 standard (unit: m)
s = Math.Round (S * 10000)/10000;
return s;
}
/**
* Calculate TP Value
* @param curpoint Current Point
* @param relatedpoint Offset Point
* @param isgeography is the geographical coordinates false 2d coordinates
* @return TP Value
*/
public static double Getdirangle (point curpoint,point relatedpoint,boolean isgeography) {
Double result = 0;
if (isgeography) {
Double y2 = Math.toradians (Relatedpoint.getlat ());
Double y1 = Math.toradians (Curpoint.getlat ());
Double alpha = math.atan2 (Relatedpoint.getlat ()-Curpoint.getlat (), (RELATEDPOINT.GETLNG ()-CURPOINT.GETLNG ()) * Math.Cos ((y2-y1)/2))//Latitude Direction times cos (Y2-Y1/2)
Double Delta =alpha<0? (2*math.pi+alpha): Alpha;
result = Math.todegrees (delta);
}else {
Double alpha = math.atan2 (Relatedpoint.getlat ()-Curpoint.getlat (), RELATEDPOINT.GETLNG ()-curpoint.getlng ());
Double delta=alpha<0? (2*math.pi+alpha): Alpha;
result = Math.todegrees (delta);
}
return result;
}
public static void Main (string[] args) {
System.out.println (Getdistance (121.446014,31.215937,121.446028464238,31.2158502442799));
}
}
The above is the entire content of this article, I hope you can enjoy.