Two latitude and longitude calculate their distance java/c#/c

Source: Internet
Author: User
Tags asin benchmark cos pow sin

calculation distance principle of latitude and longitude

interchange of latitude and longitude

Degree (DDD): E 108.90593 degrees N 34.2163 degrees

How to Degree (DDD):: 108.90593 degrees to the degree of seconds (DMS) Longitude e 108 degrees 54 minutes 22.2 seconds? The conversion method is to change 108.90593 integer digits to 108 (degrees), with 0.90593*60=54.3558 and integer digits of 54 (points), 0.3558*60=21.348 the integer digit 21 (seconds), it is converted to 108 degrees 54 minutes 21 seconds.

The same will be minutes (DMS): East longitude e 108 degrees 54 minutes 22.2 seconds Conversion degree (DDD) method is as follows: 108 degrees 54 minutes 22.2 seconds =108+ (54/60) + (22.2/3600) = 108.90616 degrees

Because of the reason of the small digit reservation in the calculation, the positive and negative calculation has some error, but the error influence is not very big. The 1-second error is a few meters. GPS car Friends can use the above method to convert to their own needs of the unit coordinates.

conversion of latitude and longitude into rice

Latitude is divided into 60 points, each divided into 60 seconds and seconds of decimals.

The latitude line projects seemingly horizontal parallel lines on the diagram, but is actually a circle of different radii. All positions at the same specific latitude are on the same weft.
The equatorial latitude is 0°, dividing the planets into the southern and northern hemispheres.
Latitude refers to the line between a point and the Earth's center and the surface of the Earth's equator, the value of which is between 0-90 degrees. The latitude at the point north of the equator is called Latitude, recorded as N, and the latitude of the point south of the equator is called Southern latitudes, recorded as S.
Latitude in the area between 0-30 degrees is called the low latitude region, latitude in the number of 30-60 degrees between the region known as the middle latitude region, latitude value of 60-90 degrees between the region known as the high latitude region.
The equator, the tropical tropics, the Tropic of Cancer, the Antarctic Circle and the Arctic Circle are special parallels.
Latitude 1 seconds of length
The total meridian length of the earth is about 40008km. Average:
Latitude 1 degrees = approximately 111km
Latitude 1 min = approximately 1.85km
Latitude 1 seconds = approximately 30.9m

calculate the distance between two points based on latitude and longitude of any two points on Earth

The earth is a near-standard ellipsoid, its equatorial radius of 6378.140 km, the polar radius of 6356.755 km, the average radius of 6371.004 km. If we assume that the earth is a perfect sphere, then its radius is the average radius of the Earth, recorded as R. If the 0-degree warp is the benchmark, then the surface distance between the two points can be calculated based on the latitude and longitude of any two points on the Earth's surface (this ignores the error of the Earth's surface topography on the calculation, only theoretical estimates). Set 1th a latitude and longitude (Lona, Lata), 2nd B latitude and longitude (lonb, LATB), according to the 0-degree longitude of the benchmark, the longitude of the positive (longitude), longitude of the degree of negative (-longitude), latitude to take 90-latitude value (90- Latitude), latitude values (90+latitude) are 90+, then two points after the above treatment are counted (Mlona, Mlata) and (Mlonb, MLATB). Then according to the triangular derivation, we can get the following formula for calculating the distance of two points:

C = sin (mlata) *sin (MLATB) *cos (mlona-mlonb) + cos (mlata) *cos (MLATB)

Distance = R*arccos (C) *pi/180

Here, R and distance units are the same, if the use of 6371.004 km as a radius, then distance is a kilometer unit, if you want to use other units, such as mile, also need to do unit conversion, 1-kilometer =0.621371192mile

If only the longitude is treated as positive or negative, and the latitude is not 90-latitude (assuming that all are in the northern hemisphere, only Australia has an applied meaning), then the formula will be:

C = sin (lata) *sin (LATB) + cos (lata) *cos (LATB) *cos (MLONA-MLONB)

Distance = R*arccos (C) *pi/180

The above can be introduced through a simple triangular transformation.

If the input and output of a trigonometric function are in radians, the formula can also be written:

C = sin (lata*pi/180) *sin (latb*pi/180) + cos (lata*pi/180) *cos (latb*pi/180) *cos ((MLONA-MLONB) *pi/180)

Distance = R*arccos (C) *pi/180


C = sin (lata/57.2958) *sin (latb/57.2958) + cos (lata/57.2958) *cos (latb/57.2958) *cos ((MLONA-MLONB)/57.2958)

Distance = R*arccos (c) = 6371.004*arccos (c) kilometer = 0.621371192*6371.004*arccos (c) mile = 3958.758349716768*arccos (c ) Mile

In practical applications, it is generally through an individual's zip code to find the corresponding area of the postal code of the latitude and longitude, and then based on these latitude and longitude to calculate the distance between each other, so as to estimate the approximate distance between certain groups of the range ( For example, the distribution of hotel passengers-each passenger's zip and longitude and latitude and longitude of the hotel calculated distance-and so on, so, through the postcode of the latitude and longitude of such a database is a very useful resource.

Java version Code

/**  *  calculates the distance between two latitude  *  @author    *  */Public class calulatetwolanlon {     private static final double earth_radius = 6378.137;// Earth radius, Unit kilometer     private static double rad (double d)    
 {        return d * Math.PI / 180.0;    &NBSP}          /**      *        *  @param  lat1  first latitude      * @ Param lng1 The first longitude      *  @param  lat2 the second latitude      *   @param  lng2 Second longitude      *  @return   two latitude and longitude distance       */    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);                  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)     {         system.out.println (Calulatetwolanlon.getdistance (22.75424,112.76535 , 23.014171, 
113.10111));    &NBSP}          }

C # version code:

private const double earth_radius = 6378.137;//Earth radius 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);
   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; }

C language Edition code:

 <summary>///  Gets the distance between two longitude and latitude/// </summary>/// <param name= "Lona" > Longitude a</param>/// <param name= "lata" > Latitude a</param>/// <param name= "LonB" > Longitude b</param>/// <param name= "LATB" > Longitude b</param>/// <returns> distance (km) </returns> Public static double getdistance (double lona, double lata,  DOUBLE&NBSP;LONB,&NBSP;DOUBLE&NBSP;LATB) {    //  East, North latitude processing, only in the domestic can not deal with (assuming that the northern hemisphere,
Only Australia in the southern hemisphere is of application significance)     double MLonA = LonA;
    double MLatA = LatA;
    double MLonB = LonB;
    double MLatB = LatB;
    //  Earth radius (km)     double R = 6371.004;     double c = math.sin (RAD (Lata))  * math.sin (Rad (LatB)  + math.cos (RAD (Lata))  * math.cos (Rad (LATB))  * math.cos (Rad (mlona - 
    return  (R * math.acos (C)); } private static double rad (double d) {    return d * 
math.pi / 180.0; }

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.