我如何找到最接近Python和Google Maps API的地方

问题描述 投票:-1回答:1

我有这个程序,我希望它从列表中返回几个地方,按顺序最接近经度和纬度的设定点。我想要它返回,例如,在我的长片和拉特元组列表中的五个最接近的位置,按顺序返回到设定点。我是用Python做的。

python google-maps google-maps-api-3 closest
1个回答
2
投票

使用:Haversine,你可以这样做:

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

center = (lon, lat)
points = [(lon1, lat1), (lon2, lat2), (lon3, lat3), (lon4, lat4), (lon5, lat5))
altogether = [list(center) + list(item) for item in points]

def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

distances = list(map(lambda a: haversine(*a), altogether))
© www.soinside.com 2019 - 2024. All rights reserved.