JAVA calculation of two latitude-longitude relationship calculation __java

Source: Internet
Author: User
Tags cos sin

Java calculates the distance between two GPs coordinate points

1. LAT1 Lung1 represents the latitude and longitude of a point, Lat2 Lung2 the latitude and longitude of B point;

2. The difference between the latitude of A=lat1–lat2 two points b=lung1-lung2 the difference of longitude of two points;

3.6378.137 is the earth radius, the unit is kilometer;

The calculated result unit is a kilometer.

The code from Google Maps's script is used to calculate the latitude and longitude between two points.

Private Const Double Earth_radius = 6378.137;
private static Double rad (double D)
{
return d * math.pi/180.0;
}
public static double Getdistance (double lat1, double lng1, double lat2, double lng2)
{
Double RADLAT1 = rad (LAT1);
Double radLat2 = rad (LAT2);
Double A = RADLAT1-RADLAT2;
Double b = rad (lng1)-Rad (LNG2);

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;
}


The following code can be used in Android to get the distance

Public double getdistance (double lat1, double lon1, double lat2, double lon2) {
Float[] Results=new float[1];
Location.distancebetween (LAT1, Lon1, LAT2, Lon2, results);
return results[0];
}


In other devices if there is no Android-like location of the Distancebetween method to use the following code to obtain

The result of this calculation is a mile, and if you want to convert it to a kilometer, multiply by 1.609344 if you need to multiply it by 0.8684.

Double distance (double lat1, double lon1, double lat2, double lon2) {
Double theta = Lon1-lon2;
Double dist = Math.sin (Deg2rad (LAT1)) * Math.sin (Deg2rad (LAT2))
+ Math.Cos (Deg2rad (LAT1)) * Math.Cos (Deg2rad (LAT2))
* Math.Cos (Deg2rad (theta));
Dist = Math.acos (dist);
Dist = rad2deg (dist);
Double miles = Dist * 60 * 1.1515;
return miles;
}

//Convert angle to radians 
static double Deg2rad (double degree) {
return degree/180 * MATH.PI;
}

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.