Calculate the Distance Based on the longitude and latitude of two points. C # and PHP code are included.

Source: Internet
Author: User
Tags asin
How do I determine the conversion of the blog. csdn. netvincent_czzarticledetails7963740 and above? The earth is constantly rotating around the Earth's axis (the Earth's axis is a hypothetical line through the earth's north and south poles and the Earth's Center), draw a large circle perpendicular to the Earth's axis in the waist, so that every point in the circle is equal to the distance between the north and south poles.

How are these twines determined by the http://blog.csdn.net/vincent_czz/article/details/7963740? The earth is constantly rotating around the Earth's axis (the Earth's axis is a hypothetical line through the earth's north and south poles and the Earth's Center), draw a large circle perpendicular to the Earth's axis in the waist, so that every point in the circle is equal to the distance between the north and south poles.

Transfer http://blog.csdn.net/vincent_czz/article/details/7963740

How are these cable cables determined? The earth is constantly rotating around the Earth's axis (the Earth's axis is a hypothetical line through the earth's north and south poles and the Earth's Center), draw a large circle perpendicular to the Earth's axis in the waist, so that every point in the circle is equal to the distance between the north and south poles. This circle is called the "Equator ". On the north and south sides of the Equator, draw many circles parallel to the equator, which are "latitude circles". The line segments that constitute these circles are called weft wires. We set the equator to zero latitude, 90 degrees to the north to the south, and north to the south of the equator.
The North Pole is 90 degrees north latitude, and the South Pole is 90 degrees south latitude. The latitude and height also indicate the hot and cold climate, such as the winter in the Equator and low latitude regions,
There is no summer in the polar and high latitudes, and the middle and latitude regions have distinct seasons.
Second, from the Arctic point to the South Pole, we can draw many large circles perpendicular to the earth's equator in the North-South direction, which is called the "Meridian Circle". The line segments that constitute these circles are called the meridian lines. In the year 1884, the international regulation adopted the longitude line of the Greenwich Observatory in the suburbs of London as the starting point for calculating the longitude, that is, 0 minutes 0 seconds, longitude, also known as the "primary meridian ". The east longitude is in the east, with a total of 180 degrees;
In the west of the Meridian, a total of 180 degrees. Because the earth is circular, the longitude of the east longitude is 180 degrees and the longitude line of the west longitude is the same. Countries
The 180-degree longitude line is the "International Date change line ". In order to avoid using two different dates in the same region, the international date variation occurs on land.
Slightly deviated.
Each longitude and latitude can be subdivided into 60 minutes, and each minute is divided into 60 seconds and decimal places in seconds. We can confirm with the cable
The specific location of each place on the earth, and it is displayed on a map or globe. For example, what is the longitude and latitude of Beijing? We
It is easy to find out from the map that the longitude is 116 degrees 24 points, and the latitude is 39 degrees 54 points. For a vessel sailing in the sea, you only need to test the longitude of its location.
To determine the position and Forward Direction of the ship in the ocean. The latitude is 90 degrees. The equator is 0 degrees, arranged to two poles, and the smaller the circle,
The higher the level.
The horizontal line is the latitude, and the vertical line is the longitude.
Of course, it can be computed by quadratic equation of four elements.
Longitude and latitude are both an angle. Longitude is the angle between two sides and the plane of the longitude line. Since all the meridian lines are of the same length, in order to determine a starting point for the longitude, after consultation at the 1884 International Conference, it was decided to go through the Greenwich Mean Royal Observatory (old site) on the outskirts of London and the south bank of the River Bank of England) the meridian line of a Main Meridian Cross is the starting meridian line, called the primary Meridian. The primary meridian plane is the start plane and the end plane is the local meridian plane. The longitude of a point is the angle between the longitude plane of the point and the primary meridian plane. On the equator, the self-Primary meridian plane is used as the starting point and measured to the east and west respectively. The value to the east is called the east longitude, and the value to the west is called the west longitude. It can be seen that the longitude of a region is the direction and angular distance of the region to the primary Meridian. The prime meridian is 0 ° longitude, the maximum value of east longitude is 180 °, the maximum value of west longitude is 180 °, and the longitude line of east longitude and west longitude is the same meridian, collectively referred to
180 ° longitude cable.
Latitude is a line angle. The START plane is the equator plane, and the line is the local ground normal. The so-called normal, that is, the line perpendicular to the reference flat surface. The latitude of a place is the angle between the normal of the place and the Equator plane. Latitude is measured on the local longitude line. It is measured from the equator to the South and the North. The North is called the north latitude, and the south is called the south latitude. It can be seen that the latitude of a region is the direction and angular distance of the region to the equator. The equator is a 0 ° weft, and the maximum value of the north latitude is 90 °, that is, the northern point; the maximum value of the south latitude is 90 °, that is, the southern pole.

Longitude and latitude Interchange

Degree (DDD): E 108.90593 degrees N 34.21630 degrees

How to convert degrees (DDD): 108.90593 degrees to degrees (DMS) Dong Jing E 108 degrees 54 minutes 22.2 seconds? The conversion method is to change the 108.90593 integer to 108 degrees, and use 0.90593*60 = 54.3558 to retrieve the entire digit 54 (points ), 0.3558*60 = 21.348 and then the integer is 21 (seconds), so it is converted to 108 degrees 54 minutes 21 seconds.

Similarly, the method for converting degrees into seconds (DMS): Dong Jing E 108 degrees 54 minutes 22.2 seconds to degrees (DDD) is as follows: 108 degrees 54 minutes 22.2 seconds = 108 + (54/60) + (22.2/3600) = 108.90616 degrees

Because the decimal point is retained During computation, there is a certain error in the positive and negative computation, but the error does not have a great impact. The error of one second is a few meters. GPS drivers can use the above method to convert the Unit coordinates they need.


Longitude and latitude to meter

The latitude is divided into 60 points, and each point is divided into 60 seconds and the decimal point of the second.

The latitude line is projected on a seemingly horizontal parallel line, but it is actually a circle with different radius. All locations with the same specific latitude are on the same weft.
The latitude of the equator is 0 °, and the planet is evenly divided into the Southern Hemisphere and the northern hemisphere.
Latitude refers to the line angle between a point and the Earth's ball center and the Earth's track surface. Its value ranges from 0 to 90 degrees. The latitude of a point located north of the equator is north latitude, which is recorded as N. the latitude of a point located south of the equator is called south latitude, and it is recorded as S.
The region with the latitude value between 0 and 30 degrees is called the low latitude area, and the region with the latitude value between 30 and 60 degrees is called the middle latitude area, the area with a latitude value between 60 and 90 degrees is called the high latitude area.
The equator, the return-to-south line, the return-to-north line, the Antarctic Circle, and the Arctic Circle are special latitudes.
Length of 1 second at latitude
The total meridian length of the Earth is about 40008 km. Average:
Latitude 1 degree = approximately 111 km
Latitude: 1 = approximately 1.85 km
Latitude 1 second = approximately 30.9 m

Calculate the distance between two points based on the longitude and latitude of any two points on the ball

The earth is a quasi-Standard Elliptical body. Its equator radius is 6378.140 km, the polar radius is 6356.755 km, and the average radius is 6371.004 km. If we assume that the Earth is a perfect sphere, its radius is the mean radius of the Earth, which is recorded as R. If the zero-degree longitude line is used as the basis, the distance between the two points can be calculated based on the latitude and longitude of any two points on the Earth's surface. (here, the error caused by the calculation of the earth's surface topography is ignored, only theoretical estimates ). Set the longitude and latitude of the first Vertex A to (LonA, LatA), and the longitude and latitude of the second vertex B to (LonB, LatB) based on the zero-degree longitude line, the Longitude is a positive value (longpolling), the Longitude is a negative value (-longpolling), and the Latitude is 90-Latitude ), when the Latitude value is 90 + (90 + Latitude), the two points after processing are counted as (MLonA, MLatA) and (MLonB, MLatB ). According to the triangle derivation, the following formula is obtained for calculating the distance between two points:

C = sin (MLatA) * sin (MLatB) * cos (MLonA-MLonB) + cos (MLatA) * cos (MLatB)

Distance = R * Arccos (C) * Pi/180

Here, the units of R and Distance are the same. If 6371.004 km is used as the radius, Distance is the unit of kilometer. If other units such as mile are used, unit conversion is also required, 1Km = 0.621371192 mile

If only the longitude is processed plus and minus, And the Latitude is not processed with 90-Latitude (assuming they are all in the northern hemisphere, and the southern hemisphere is only available in Australia), 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 simple triangle transformation.

If the input and output values of trigonometric functions use radians, the formula can also be written as follows:

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, we generally use an individual zip code to find the longitude and latitude of the region center corresponding to the zip code, and then calculate the distance between each other based on these latitudes, to estimate the approximate distance between certain groups (for example, the distribution range of hotel passengers-the latitude and longitude corresponding to the zip code of each passenger and the distance range calculated by the hotel's latitude and longitude ), therefore, it is a useful resource to query such a database by postal code.

Appendix: C # code:

[Csharp]View plaincopyprint?

  1. Private const double EARTH_RADIUS = 6378.137; // Earth radius
  2. Private static double rad (double d)
  3. {
  4. Return d * Math. PI/180.0;
  5. }
  6. Public static double GetDistance (double lat1, double lng1, double lat2, double lng2)
  7. {
  8. Double radLat1 = rad (lat1 );
  9. Double radLat2 = rad (lat2 );
  10. Double a = radLat1-radLat2;
  11. Double B = rad (lng1)-rad (lng2 );
  12. Double s = 2 * Math. Asin (Math. Sqrt (Math. Pow (Math. Sin (a/2), 2) +
  13. Math. Cos (radLat1) * Math. Cos (radLat2) * Math. Pow (Math. Sin (B/2), 2 )));
  14. S = s * EARTH_RADIUS;
  15. S = Math. Round (s * 10000)/10000;
  16. Return s;
  17. }




Appendix: PHP code:

[Php]View plaincopyprint?

  1. Function rad ($ d)
  2. {
  3. Return $ d * 3.1415926535898/180.0;
  4. }
  5. Function GetDistance ($ lat1, $ lng1, $ lat2, $ lng2)
  6. {
  7. $ EARTH_RADIUS = 6378.137;
  8. $ RadLat1 = rad ($ lat1 );
  9. // Echo $ radLat1;
  10. $ RadLat2 = rad ($ lat2 );
  11. $ A = $ radLat1-$ radLat2;
  12. $ B = rad ($ lng1)-rad ($ lng2 );
  13. $ S = 2 * asin (sqrt (pow (sin ($ a/2), 2) +
  14. Cos ($ radLat1) * cos ($ radLat2) * pow (sin ($ B/2), 2 )));
  15. $ S = $ s * $ EARTH_RADIUS;
  16. $ S = maid ($ s * 10000)/10000;
  17. Return $ s;
  18. }

 
PS: This article according to the http://www.cnblogs.com/ycsfwhh/archive/2010/12/20/1911232.html and http://blog.csdn.net/alexjames_83/article/details/4261403 integrated editing.

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.