Content
Haversine公式
Π 180 ∗ d e g = r a d \frac{\Pi}{180}* deg = rad 180Π∗deg=rad
h a v e r s i n ( θ ) = s i n 2 ( θ 2 ) = 1 − c o s ( θ ) 2 haversin(\theta) = sin^2(\frac{\theta}{2}) = \frac{1-cos(\theta)}{2} haversin(θ)=sin2(2θ)=21−cos(θ)
h a v ( d r ) = h a v ( φ 1 − φ 2 ) + c o s ( φ 1 ) c o s ( φ 2 ) h a v ( λ 1 − λ 2 ) hav(\frac{d}{r}) = hav(\varphi_1 - \varphi_2) + cos(\varphi_1)cos(\varphi_2)hav(\lambda_1 - \lambda_2) hav(rd)=hav(φ1−φ2)+cos(φ1)cos(φ2)hav(λ1−λ2)
d = 2 ∗ r ∗ a r c s i n ( s i n 2 ( φ 1 − φ 2 2 ) + c o s ( φ 1 ) c o s ( φ 2 ) s i n 2 ( λ 1 − λ 2 2 ) ) d = 2*r*arcsin(\sqrt{ sin^2(\frac{\varphi_1 - \varphi_2}{2} ) + cos(\varphi_1)cos(\varphi_2) sin^2(\frac{\lambda_1 - \lambda_2}{2} ) }) d=2∗r∗arcsin(sin2(2φ1−φ2)+cos(φ1)cos(φ2)sin2(2λ1−λ2))
d is the distance between two points
r is the radius of the earth
φ
1
φ
2
{\displaystyle \varphi _{1}\varphi _{2}}
φ1φ2 is the latitude at point 1 and the latitude at point 2
λ
1
λ
2
{\displaystyle \lambda _{1}\lambda _{2}}
λ1λ2 is the Longitude at point 1 and longitude at point 2
python 代码实现
ng1,lat1 为开始点的经纬度
ng2,lat2 为结束点的经纬度
def geodistance(lng1,lat1,lng2,lat2):
#lng1,lat1,lng2,lat2 = (120.12802999999997,30.28708,115.86572000000001,28.7427)
lng1, lat1, lng2, lat2 = map(radians, [float(lng1), float(lat1), float(lng2), float(lat2)]) # 经纬度转换成弧度
dlon=lng2-lng1
dlat=lat2-lat1
a=sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
distance=2*asin(sqrt(a))*6371*1000 # 地球平均半径,6371km
distance=round(distance/1000,3)
return distance