gpt4 book ai didi

python - 如何在python中从给定的纬度和经度找到500米以内的用户位置

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:12 25 4
gpt4 key购买 nike

我想在 Python 中找到距给定经纬度 500 米以内的用户位置。

给定纬度和经度 = 19.114315,72.911174

我想检查新的纬度和经度是否在给定纬度和经度的 500 米范围内..

新经纬度 = 19.112398,72.912743

我在 python 中使用这个公式..

math.acos(math.sin(19.114315) * math.sin(19.112398) + math.cos(19.114315) * math.cos(19.112398) * math.cos(72.912743 - (72.911174))) * 6371 <= 0.500 

但它没有给我预期的结果..我错过了什么吗?请帮助..

最佳答案

您可以使用 Haversine 公式获取两点之间的大圆距离(沿球体)。将地球视为远距离的球体存在一些问题,但对于 500 米,你可能没问题(假设你不想将医疗包裹丢在船上或其他东西上)。

from math import radians, sin, cos, asin, sqrt

def haversine(lat1, long1, lat2, long2, EARTH_RADIUS_KM=6372.8):

# get distance between the points
phi_Lat = radians(lat2 - lat1)
phi_Long = radians(long2 - long1)

lat1 = radians(lat1)
lat2 = radians(lat2)

a = sin(phi_Lat/2)**2 + \
cos(lat1) * cos(lat2) * \
sin(phi_Long/2)**2

c = 2 * asin(sqrt(a))
return EARTH_RADIUS_KM * c

如果两点之间的距离小于您的阈值,则在范围内:

points_1 = (19.114315,72.911174)
points_2 = (19.112398,72.912743)
threshold_km = 0.5


distance_km = haversine(points_1[0], points_1[1], points_2[0], points_2[1])
if distance_km < threshold_km:
print('within range')
else:
print('outside range')

关于python - 如何在python中从给定的纬度和经度找到500米以内的用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34262872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com