【问题标题】:Is there api for giving me a list of directions for traveling from city A to city B [closed]是否有 api 可以给我一个从城市 A 到城市 B 的路线列表 [关闭]
【发布时间】:2015-08-15 23:32:18
【问题描述】:

所以我有这个 python 程序,它输入了城市 A 和城市 B 的 longitude latitude。是否有一个 api 可以输出从城市 A 到城市 B 的路线字符串。就像这样的输出:

Turn right on South DeAnza Blvd and then make a turn........ This is your destination.

谁能给我任何指导?任何帮助将不胜感激。

【问题讨论】:

    标签: python maps directions


    【解决方案1】:

    您可以使用Google Directions API

    Google Directions API 是一项计算路线的服务 使用 HTTP 请求在不同位置之间。

    一个简单的例子:

    import urllib2
    import json
    
    url = "https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal"
    response = urllib2.urlopen(url)
    j = json.loads(response.read())
    
    for step in j['routes'][0]['legs'][0]['steps']:
        print step['html_instructions']
    

    输出:

    Head <b>north</b> on <b>Bay St</b> toward <b>Hagerman St</b>
    Turn <b>right</b> onto <b>Dundas St W</b>
    Turn <b>left</b> onto the <b>Don Valley Parkway</b> ramp Merge onto <b>Don Valley Pkwy N</b>
    Take the <b>ON-401 E</b> exit Merge onto <b>Ontario 401 Express</b>
    Merge onto <b>ON-401 E</b>
    Keep <b>left</b> at the fork to continue on <b>ON-401</b>
    Continue onto <b>Autoroute du Souvenir/Autoroute 20</b>
    <div style="font-size:0.9em">Entering Québec</div>
    Keep <b>left</b> to continue on <b>Boulevard Ville-Marie/Autoroute 720 E</b>
    Keep <b>right</b> to stay on <b>Boulevard Ville-Marie/Autoroute 720 E</b>
    Take exit <b>4</b> toward <b>Rue de la Montagne N/Rue Saint-Jacques</b>
    Turn <b>left</b> onto <b>Jean d'Estrees St</b>
    Turn <b>right</b> onto <b>St Antoine St W</b>
    Turn <b>left</b> onto <b>Rue Mansfield</b>
    Turn <b>right</b> onto <b>René-Lévesque Blvd W</b>
    Turn <b>right</b> onto <b>Boulevard Robert-Bourassa</b>
    <div style="font-size:0.9em">Destination will be on the right</div>
    

    【讨论】:

    • 您的代码仅适用于从多伦多到蒙特利尔。我有来自地址 A 的输入和来自地址 B 的输入,我需要从 A 到 B 的路线。
    • 你知道如何在代码中自定义起点和终点吗?
    • @TheAndruino:代码只是一个例子。您应该根据需要对其进行修改。请阅读答案中的链接文档。
    • 如何将纬度和经度转换为 placeID?
    猜你喜欢
    • 1970-01-01
    • 2011-08-12
    • 2012-08-05
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多