请写一个Python程序程序的功能是计算两个给定经纬度的坐标点之间的距离
以下是一个计算两个经纬度之间距离的Python程序:
import math
def distance(lat1, lon1, lat2, lon2):
lat1 = math.radians(lat1)
lon1 = math.radians(lon1)
lat2 = math.radians(lat2)
lon2 = math.radians(lon2)
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.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = 6371 * c
return distance
这个程序使用了Haversine公式来计算两个经纬度之间的距离。输入的参数是四个经纬度值,分别是第一个点的纬度和经度,以及第二个点的纬度和经度。程序会将这些值转换为弧度,并使用Haversine公式计算它们之间的距离。最后,程序将距离乘以地球半径(6371公里)以得出最终的距离,以公里为单位。
原文地址: https://www.cveoy.top/t/topic/bvh0 著作权归作者所有。请勿转载和采集!