2013-06-03 96 views
0

我用http://maps.googleapis.com/maps/api/geocode/json得到纬度和长度 并保存到数据库中。 我正在尝试编写计算两个位置之间的代码。 是否有任何计算两个距离之间距离的好API。 我在亚洲和欧洲有一些地点。 谷歌距离只返回行驶距离我想有一个实际的距离。 预先感谢您。我该如何计算两个位置之间的距离

+4

为什么你需要的API?这是一个公式,你可以用谷歌查找。这只是球形三角。 – Barmar

+0

你不需要API,只需要Haversine公式。 –

+0

根据你的数据库 - 它甚至可能有功能/指标,为你做这个... –

回答

2

您可以通过使用haversine公式

这里计算的距离,origindestination是纬度,经度元组

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 
相关问题