Java implementation calculates the distance between geographic coordinates _java

Source: Internet
Author: User
Tags cos pow sin

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.

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.