如何在给定的lat和500以内的python中找到用户位置

我想在距离给定的lat和500长度的 Python内找到一个用户位置.

鉴于lat& long = 19.114315,72.911174

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

new lat and long = 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')
点赞