Python 以知经纬度坐标,基于haversine公式求两点间距离

Haversine公式

haversine公式可以通过经纬度和弧度的转变,然后得到地球上两点的距离。

# 经度,纬度
def Geo_Distance(lng_1, lat_1, lng_2, lat_2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)

    :return: the distance between two points on the earth
    """
    # 十进制转化为弧度制
    lng_1, lat_1, lng_2, lat_2 = map(radians, [lng_1, lat_1, lng_2, lat_2])

    # Haversine公式
    dlng = lng_2 - lng_1
    dlat = lat_2 - lat_1
    a = sin(dlat / 2) ** 2 + cos(lat_1) * cos(lat_2) * sin(dlng / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371 # 地球半径

    mNear = c * r * 1000 # 单位为m的长度

    return mNear

你可能感兴趣的:(python)