如何用经纬度计算两地的距离
- 编程技术
- 2025-01-27 20:12:05
- 1
使用经纬度计算两地之间的距离,通常可以使用球面三角学中的公式。以下是两种常用的方法: 1. Haversine 公式Haversine 公式是计算地球上两点间最短距离的...
使用经纬度计算两地之间的距离,通常可以使用球面三角学中的公式。以下是两种常用的方法:
1. Haversine 公式
Haversine 公式是计算地球上两点间最短距离的一种方法,它适用于球面。公式如下:
```plaintext
a = sin2(Δφ/2) + cos φ1 ? cos φ2 ? sin2(Δλ/2)
c = 2 ? atan2(√a, √(1?a))
d = R ? c
```
其中:
φ1 和 φ2 是两点的纬度(以弧度为单位)。
λ1 和 λ2 是两点的经度(以弧度为单位)。
Δφ = φ2 φ1
Δλ = λ2 λ1
R 是地球的平均半径,大约为 6371 公里。
d 是两点之间的距离。
在 Python 中,你可以使用以下代码来计算距离:
```python
import math
def haversine_distance(lat1, lon1, lat2, lon2):
R = 6371 地球半径,单位为公里
phi1, phi2 = math.radians(lat1), math.radians(lat2)
delta_phi = math.radians(lat2 lat1)
delta_lambda = math.radians(lon2 lon1)
a = math.sin(delta_phi / 2)2 + math.cos(phi1) math.cos(phi2) math.sin(delta_lambda / 2)2
c = 2 math.atan2(math.sqrt(a), math.sqrt(1 a))
distance = R c
return distance
示例
distance = haversine_distance(34.052235, -118.243683, 40.712776, -74.005974)
print(f"Distance: {distance
本文链接:http://www.xinin56.com/bian/364902.html
上一篇:资源稀缺性指的是什么
下一篇:一伸一缩还有哪些词语