according to 2 a latitude and longitude point, calculate this 2 distance between a latitude and longitude point
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.
according to 2 latitude and longitude coordinates, distance calculation function
The following is a function that calculates the distance between spherical two points (LAT1, Lon1)-(LAT2, Lon2).
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceharvensin{classProgram {/// <summary> ///calculates the distance between 2 points based on latitude and longitude. /// </summary> /// <param name= "args" ></param> Static voidMain (string[] args) { //39.94607,116.32793 31.24063,121.42575Console.WriteLine (Distance (39.94607,116.32793,31.24063,121.42575)); } Public Static DoubleHaversin (DoubleTheta) { varv = Math.sin (theta/2); returnV *v; } Static DoubleEarth_radius =6371.0;//km Earth radius average, km /// <summary> ///given longitude 1, latitude 1, longitude 2, Latitude 2. Calculates the distance between 2 latitude and longitude. /// </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 (kilometers, kilometers)</returns> Public Static DoubleDistance (DoubleLAT1,DoubleLon1,DoubleLAT2,DoubleLon2) { //the distance between the spherical two points is calculated using the Haversine formula. //latitude and longitude converted into radiansLAT1 =Convertdegreestoradians (LAT1); Lon1=Convertdegreestoradians (Lon1); LAT2=Convertdegreestoradians (LAT2); Lon2=Convertdegreestoradians (Lon2); //Difference Value varVlon = Math.Abs (Lon1-lon2); varVlat = 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); varDistance =2* Earth_radius *Math.asin (Math.sqrt (h)); returndistance; } /// <summary> ///Convert the angle to radians. /// </summary> /// <param name= "degrees" >Angle</param> /// <returns>radians</returns> Public Static DoubleConvertdegreestoradians (Doubledegrees) { returnDegrees * Math.PI/ the; } Public Static DoubleConvertradianstodegrees (Doubleradian) { returnRadian *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.
2) Another method:
SQL Server is itself a spatial data index (spatial indexing) with spatial data computing power.
He implements these functions through an extension DLL, Microsoft.SqlServer.Types.dll . This is a managed DLL, which means that. NET C # ASP can also use some features.
For example by reference reference: Microsoft.SqlServer.Types.dll this DLL.
var a = Sqlgeography.point (22.54587746114.128730774326// some point in Shanghai var b = Sqlgeography.point (at some point in4326// Shanghai, 4236 represents the WGS84 coordinate reference system. // distance
This calculated distance, with the above using the Haversine formula calculated distance, the error within a few meters.
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