Home >Common Problem >[Mathematical formulas and derivation] Calculate the distance between two points on the ground based on longitude and latitude

[Mathematical formulas and derivation] Calculate the distance between two points on the ground based on longitude and latitude

藏色散人
藏色散人forward
2020-10-22 11:38:493639browse

1. Assumption: The earth is a perfect sphere. The longitude and latitude coordinates of two points A and B on the ground are (Aj, Aw) and (Bj, Bw) respectively. The average radius of the earth R is 6371km.

2. Establish a three-dimensional rectangular coordinate system:

The center of the earth is the origin O, the earth's axis is the Z axis, the north pole direction is the positive direction of the Z axis, and the equatorial plane is X The plane where the axis and the Y-axis are located, on this plane, the direction from the center of the earth to the zero-degree longitude is the positive direction of the X-axis, and the positive direction of the Y-axis is determined according to the right-hand rule.

Suppose the three-dimensional coordinates of point A are (Ax, Ay, Az), and the three-dimensional coordinates of point B are (Bx, By, Bz)

3. Idea :

The plane where the three points A, B, and O intersects with the earth forms a circle with a radius R. To find the ground distance between AB is to find the length of the arc AB on the circle. It can be found by the formula that the arc length is equal to the radius multiplied by the central angle.

Since R is determined, the length of arc AB can be obtained as long as the angle θ between OA and OB is obtained. Arc AB=R*θ.
The angle θ can be obtained through the vector formula:

Vector OA*vector OB=|OA||OB|cosθ.
then

cosθ=向量OA*向量OB/|OA||OB|
     =(Ax*Bx+Ay*By+Az*Bz)/R*R

4. Use longitude and latitude coordinates to express the three-dimensional rectangular coordinates:

Ax=R*cosAw*cosAj
Ay=R*cosAw*sinAj
Az=R*sinAw
Bx=R*cosBw*cosBj
By=R*cosBw*sinBj
Bz=R*sinBw

Substitute in to get

cosθ=cosAw*cosAj*cosBw*cosBj+cosAw*sinAj*cosBw*sinBj+sinAw*sinBw
     =cosAw*cosBw(cosAj*cosBj+sinAj*sinBj)+sinAw*sinBw
     =cosAw*cosBw*cos(Aj-Bj)+sinAw*sinBw
θ=arccos[cosAw*cosBw*cos(Aj-Bj)+sinAw*sinBw]

5. In summary, it can be calculated based on longitude and latitude The formula for the distance between two points on the ground:

弧AB=R*arccos[cosAw*cosBw*cos(Aj-Bj)+sinAw*sinBw]

Description:

Everyone has done the derivation of similar formulas before. After a long time, you may forget some things, so I wrote it down. Saved for reference.

Due to the many transformations of trigonometric functions, the formula can be expressed in more than one form. As long as the premise assumptions are the same, they should be mathematically equivalent and can be deduced from each other.
When using it in actual program code, you need to pay attention to unit issues. For example, the unit of longitude and latitude obtained from the locator is degrees, and the trigonometric calculation method is likely to use radians. Another example is whether the unit of length is km or m. wait.

Recommended: "Programming Video"

The above is the detailed content of [Mathematical formulas and derivation] Calculate the distance between two points on the ground based on longitude and latitude. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete