python 已知一点坐标(经纬度),如何判断其方圆500米的范围?
时间: 2023-05-21 07:00:47 浏览: 69
可以使用 haversine 公式计算两个经纬度之间的距离,然后判断距离是否小于等于 500 米。具体实现可以参考以下代码:
```python
import math
def distance(lat1, lon1, lat2, lon2):
R = 6371 # 地球半径,单位为公里
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = math.sin(dLat / 2) * math.sin(dLat / 2) + \
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * \
math.sin(dLon / 2) * math.sin(dLon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c * 1000 # 返回距离,单位为米
# 示例坐标
lat1, lon1 = 39.9087, 116.3975
# 判断另一个点是否在 500 米范围内
lat2, lon2 = 39.9087, 116.401
if distance(lat1, lon1, lat2, lon2) <= 500:
print('在范围内')
else:
print('不在范围内')
```
注意,这里的距离计算是基于球体模型的,实际上地球是椭球体,因此在极端情况下可能会有一定误差。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)