地球上兩個點 (經緯度)之間的距離
def haversine(lat1,lon1,lat2,lon2): # 經度1,緯度1,經度2,緯度2 (十進制度數)
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# 將十進制度數轉化為弧度
lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
# haversine公式
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
r = 6371 # 地球平均半徑,單位為公里
return c * r

浙公網安備 33010602011771號