【发布时间】:2013-05-30 02:07:20
【问题描述】:
我正在尝试使用网上找到的这个函数来计算地球上两个经纬度点之间的距离。
但是,我不知道如何传递我的纬度和经度值,因为我替换了 lat1、lon1 和 lat2、lon2,并且每次都会出错。
我在哪里输入我想要的纬度和经度值?
import math
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
编辑: 例如,如果我有
lat1 = 20 and lon1 = 100
lat2 = 30 and lon2 = 110
为什么我用函数中的数字替换 lon1 会失败?
【问题讨论】:
标签: python function printing formula