已知AB两点点经纬度求两点距离公式
时间: 2024-05-05 12:15:14 浏览: 112
地球是一个近似的球体,因此可以使用球面三角学公式来计算两点间的距离。
设点A的经纬度为 (lon1, lat1),点B的经纬度为 (lon2, lat2),则它们之间的距离 d 可以计算为:
d = R * arccos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1))
其中,R 是地球的平均半径(约为 6371km),arccos 是反余弦函数,sin 和 cos 分别是正弦和余弦函数,lat1、lat2、lon1、lon2 的单位是弧度(通常经纬度以度数表示,需要先转换为弧度)。
需要注意的是,由于计算中使用了反余弦函数,因此计算结果的单位也是弧度,需要转换为实际距离单位(如千米或英里)。
下面是 Python 代码示例:
```python
from math import radians, cos, sin, asin, sqrt
def distance(lon1, lat1, lon2, lat2):
R = 6371 # 地球平均半径,单位为千米
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) # 将经纬度转换为弧度
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
distance = R * c
return distance
```
其中,lon1、lat1、lon2、lat2 分别是两点的经纬度,函数返回的是两点之间的距离(单位为千米)。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)