def distance(lat1,lon1,lat2,lon2):
lat1=lat1*math.pi/180
lon1=lon1*math.pi/180
lat2 = lat2 * math.pi / 180
lon2=lon2*math.pi/180
dis=math.acos(round((math.sin(lat1)*math.sin(lat2))+(math.cos(lat1)*math.cos(lat2)* math.cos(lon2-lon1)),10))*6371.004
#dis = math.acos((math.sin(lat1) * math.sin(lat2)) + (math.cos(lat1) * math.cos(lat2) * math.cos(lon2 - lon1))) * 6371.004
#由于acos的定义域为【-1,1】,当两个点为相同的点时,acos()内的计算结果理论上应为1,但实际保存的结果可能大于1,引发ValueError: math domain error错误
#这里使用round()对acos()内的结果进行小数截留
return dis
经纬度计算球面距离,ValueError: math domain error错误
最新推荐文章于 2024-07-28 07:00:00 发布