Calculates the distance between two points based on latitude and longitude

Source: Internet
Author: User

The earth is a nearly standard ellipsoid with an equatorial radius of 6378.140 km, a polar radius of 6356.755 km and an 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 meridian is used as a benchmark, 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 caused by the Earth's surface topography, which is only a theoretical estimate). The latitude and longitude of 1th A is (Lona, LatA), the latitude and longitude of 2nd B is (LONB, LATB), according to the datum of 0 degrees longitude, the positive value of longitude (longitude), the longitude is negative (-longitude), north latitude takes 90-latitude value (90- Latitude), 90+ latitude value (90+latitude), the two points after the above treatment are counted as (Mlona, Mlata) and (Mlonb, MLATB). Then, according to triangular derivation, we can get the following formula to calculate 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 the unit, if you want to use other units, such as mile, also need to do unit conversion, 1-kilometer =0.621371192mile

If the longitude is treated only as positive or negative, and the latitude is not 90-latitude (assuming that the northern hemisphere is the only application in the southern hemisphere), 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 trigonometric functions are in radians, then 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

That is

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 application, it is generally through an individual zip code to find the corresponding regional center of 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-the latitude and longitude of each traveler's postal code and the distance range computed by the latitude and longitude of the hotel-and so on, so a database is a useful resource for querying latitude and longitude by postal code. Attached: C # code:

Private Const DoubleEarth_radius =6378.137;//Earth radiusPrivate Static DoubleradDoubled) {   returnD * Math.PI/180.0;} Public Static DoubleGetdistance (DoubleLAT1,DoubleLng1,DoubleLAT2,Doublelng2) {   DoubleRADLAT1 =Rad (LAT1); DoubleRADLAT2 =Rad (LAT2); DoubleA = RADLAT1-RadLat2; Doubleb = rad (lng1)-Rad (lng2); Doubles =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; returns;}

Calculates the distance between two points based on latitude and longitude

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.