Python 如何计算两个位置之间的距离

Python 如何计算两个位置之间的距离,python,Python,我过去常常长时间呆在家里 并保存到数据库中。 我正在尝试编写在两个位置之间进行计算的代码。 有没有好的api可以计算两个距离之间的距离。 我在亚洲和欧洲有一些办事处。 Google distance只返回行驶距离,我想要一个实际距离。 提前谢谢。您可以使用哈弗森公式计算距离 这里,origin和destination是lat,长元组 import math def haversine(origin, destination): lat1, lon1 = origin lat2

我过去常常长时间呆在家里 并保存到数据库中。 我正在尝试编写在两个位置之间进行计算的代码。 有没有好的api可以计算两个距离之间的距离。 我在亚洲和欧洲有一些办事处。 Google distance只返回行驶距离,我想要一个实际距离。
提前谢谢。

您可以使用哈弗森公式计算距离

这里,
origin
destination
是lat,长元组

import math

def haversine(origin, destination):

    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371

    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

c1 = [43.47066971,-80.54305153]
c2 = [43.46745,-80.54319]

print haversine(c1, c2) # 0.358189763734

为什么需要API?这是一个你可以在谷歌上查到的公式。这只是球面三角法。你不需要API,只需要哈弗森公式。根据你的数据库,它甚至可能有函数/索引为你做这件事。。。