Calculates the distance between the 2 latitude and longitude points based on the 2 latitude and longitude points

Source: Internet
Author: User

Original http://www.cnblogs.com/softfair/p/distance_of_two_latitude_and_longitude_points.html

Calculates the distance between the 2 latitude and longitude points based on the 2 latitude and longitude points

The formula for calculating the distance between any two points on a sphere can be found in the following articles on Wikipedia.

    • Great-circle distance
    • Haversine formula

It is worth mentioning that Wikipedia recommends the use of the Haversine formula, the reason is that the great-circle distance formula uses a large number of cosine functions, and the distance between two points is very short (for example, the Earth's surface is hundreds of meters apart two points), the cosine function will be drawn 0.999 ... Result in a large rounding error. And the Haversine formula uses the sine function, even if the distance is very small, can maintain enough valid numbers. In the past, the calculation of trigonometric tables does have this problem, but after practical verification, using the computer to calculate, the difference between the two formulas is not big. To be safe, the Haversine formula is still used here.

which

    • R is the radius of the earth, the desirable mean 6371km;
    • Φ1, φ2 represents the latitude of two points;
    • Δλ represents the difference between two points of longitude.

Distance calculation function based on 2 latitude and longitude coordinates

The

Below is a function that calculates the distance between spherical two points (LAT1, Lon1)-(LAT2, Lon2).

Using system;using system.collections.generic;using system.linq;using system.text;namespace HarvenSin{class Program  {//<summary>///The distance between 2 points is calculated based on latitude and longitude. </summary>//<param name= "args" ></param> static void Main (string[] args) {//39.94607,116.  32793 31.24063,121.42575 Console.WriteLine (Distance (39.94607, 116.32793, 31.24063, 121.42575));      } public static double Haversin (double theta) {var v = math.sin (THETA/2);  Return v * v; } static Double Earth_radius = 6371.0;//km Earth radius Average, km///<summary>//////For a given longitude 1, latitude 1, longitude 2, Latitude 2.  Calculates the distance between 2 latitude and longitude degrees.  </summary>//<param name= "LAT1" > Longitude 1</param>//<param name= "Lon1" > Latitude 1</param>// <param name= "Lat2" > Longitude 2</param>//<param name= "Lon2" > Latitude 2</param>//<returns> distance (km, KM) </returns> public static double Distance (double lat1,double lon1, double lat2,double lon2) {//with Haversine      To calculate the distance between the spherical two points. Warp latitudeConvert to radians LAT1 = Convertdegreestoradians (LAT1);      Lon1 = Convertdegreestoradians (Lon1);      LAT2 = Convertdegreestoradians (LAT2);      Lon2 = Convertdegreestoradians (Lon2);      difference var Vlon = Math.Abs (Lon1-lon2);      var Vlat = Math.Abs (LAT1-LAT2);      The great circle distance in radians, a great circle is a facet on a sphere whose center is the largest circle in the perimeter of the globe.      var h = haversin (Vlat) + math.cos (LAT1) * MATH.COS (LAT2) * Haversin (Vlon);      var distance = 2 * Earth_radius * Math.asin (MATH.SQRT (h));  return distance;  }///<summary>//convert angle to radians. </summary>//<param name= "degrees" > Angle </param>//<returns> radians </returns> public STA  Tic double Convertdegreestoradians (double degrees) {return degrees * math.pi/180;  } public static double Convertradianstodegrees (double radian) {return radian * 180.0/MATH.PI; }    }}

The origin of the formula:

Versine (f) =1-cos (f)

The origin of Haversine's name is Ha-versine, or half-versine, which means half of sin.

Hav (a) = (1-cos (a))/2 = sin (a/2) * sin (A/2)

Demolition Process:

such as the next circle with a radius of 1, O is the center of the Circle, A, B is the chord (chord). Angle Aob=theta. The angle AOC=THETA/2. OC is perpendicular to AB's vertical line (perpendicular). The AC length is sin (THETA/2) and the AB length is 2*sin (THETA/2).

(Fig. 1)

As shown in the Earth diagram below, assuming that radius r is 1,o is the sphere, a (Lat1,lon1) and B (Lat2,lon2) are 2 points of interest to us. 2 The Longitude line lon1,lon2 intersects the Arctic (North Pole) N. The line where EF is located is the equator (equator). The ACBD is a four vertex (vertice) of the isosceles trapezoid on the plane. The strings of AC and db (straight lines) are not drawn on the graph. The CD location is: C (lat2,lon1) and D (Lat1,lon2). The angle AOC is the latitude difference between point A and point C dlat. The angle EOF is the difference between the longitude e point and the longitude F point dlon.

(Fig. 2)

The length of the string AC, referring to Figure 1, then the Ac=2*sin (DLAT/2), chord BD is the same length.

E, F 2 points are 2 points on the equator, and their latitude is 0. The distance to EF is Ef=2*sin (DLON/2)

A, the latitude of D2 point is LAT1. The radius of the circle plane at the latitude of the ad is the cos (LAT1). A vertical line from a (perpendicular) to OE for Ag,ao is the sphere radius, then Og=cos (LAT1), which is the radius (AO ') of the latitude circle where a and d are located.

At this time, the ad chord length ad= 2*sin (DLON/2) *cos (LAT1), similar can be introduced CB length = Cb=2*sin (DLON/2) *cos (LAT2)

Now look at how to find the length of AB, back to the plane isosceles trapezoid, such as:

(Fig. 3)

Ah is the vertical line to the CB (perpendicular), ch= (CB-AD)/2.

According to the Pythagorean Theorem (Pythagorean theorem): "^2 represents 2 squared"

ah^2 = ac^2-ch^2

= ac^2-(cb-ad) ^2/4

The length of the HB is Hb=ad+ch = AD + (cb-ad)/2 = (CB+AD)/2, which is obtained according to the Pythagorean theorem:

ab^2 = ah^2 + hb^2

= ac^2-(cb-ad) ^2/4 + (Cb+ad) ^2/4

= ac^2 + Cb*ad

We have obtained the lengths of the AC, AD and CB according to the distance of the longitude and latitude on the front spherical surface, substituting the formula to obtain:

ab^2 = sin^2 (DLAT/2) + 4*cos (LAT1) *cos (LAT2) *sin^2 (DLON/2))

Suppose the median h is half the square of the AB length, as follows

h = (AB/2) ^2

= (sin^2 (DLAT/2)) + cos (LAT1) * cos (LAT2) * SIN^2 (DLON/2)

(see h in the code)

The final step is to obtain the angle AOB representing the AB length. By referring to Figure 1, we can see how

(Fig. 4)

Set ac=, according to the Pythagorean Theorem (Pythagorean theorem) obtained:

oc= = sqrt (oa^2-ac^2)

= = sqrt (1-a)//sqrt denotes open radical

If set C is the degree value of the angular AOB.

Tan (<AOC) = tan (c) = Ac/oc = sqrt (a)/sqrt (1-a)

The

c = 2 * arctan (sqrt (a)/sqrt (1-a)),

The final ab true distance, take the earth radius on it.

Distance = 2 * Earth_radius * c.

End.

Resources:

Http://mathforum.org/library/drmath/view/51879.html

http://blog.charlee.li/location-search/

Calculates the distance between the 2 latitude and longitude points based on the 2 latitude and longitude points

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.