【python實(shí)戰(zhàn)】批量獲得路徑規(guī)劃——高德地圖API

1.需求

【python實(shí)戰(zhàn)】 批量獲取經(jīng)緯度-高德地圖API

在上篇中,已經(jīng)批量獲得了經(jīng)緯度信息,現(xiàn)在需要根據(jù)經(jīng)緯度來(lái)進(jìn)行路徑規(guī)劃,想知道兩點(diǎn)之間的距離和路程、花費(fèi)等信息。

這時(shí)候就需要用到高德地圖API中的路徑規(guī)劃功能了。

2.過(guò)程

1. 構(gòu)造經(jīng)緯度函數(shù)

同上篇,先構(gòu)造出獲得經(jīng)緯度函數(shù)便于調(diào)用。

import requests,json
# 返回經(jīng)緯度
def gain_location(adress):
    api_url=f'https://restapi.amap.com/v3/geocode/geo?city=北京市&address={adress}&key=自己的key&output=json&callback=showLocation'
    r = requests.get(api_url)
    r = r.text
    r = r.strip('showLocation(')#高德
    r = r.strip(')')
    jsonData = json.loads(r)['geocodes'][0]['location'] # 將json字符串轉(zhuǎn)換為字典類型轉(zhuǎn)為字典格式類型
    return jsonData

2.構(gòu)造路徑規(guī)劃函數(shù)

理解了上篇的請(qǐng)求參數(shù),路徑規(guī)劃就很好理解了。文檔中也有詳細(xì)解釋,路徑規(guī)劃中包括步行、公交、駕車(chē)、騎行、貨車(chē)路徑規(guī)劃以及距離測(cè)量。

這里以公交路徑規(guī)劃為例。

公交路徑規(guī)劃.png

根據(jù)必填項(xiàng):自己申請(qǐng)到的Key,起終點(diǎn)的經(jīng)緯度以及城市,就可以返回相關(guān)的字段信息。

# 路徑規(guī)劃
def get_route(origin,destination):
    api=f'https://restapi.amap.com/v3/direction/transit/integrated?origin={origin}&destination={destination}&output=JSON&key=自己的key&city=北京'
    r=requests.get(api)
    r=r.text
    jsonData=json.loads(r)
    return jsonData
公交返回結(jié)果.png

3.構(gòu)造返回信息函數(shù)

根據(jù)返回結(jié)果參數(shù),可以提取很多關(guān)于路徑規(guī)劃的信息,這里以起終點(diǎn)步行距離,路線出租車(chē)費(fèi)用,路線時(shí)間,路線費(fèi)用,路線距離為例。

def get_route_info(start,end):    
    routeplan=[]
    for o in start:
        for d in end:
            route=[]
            #起點(diǎn)
            route.append(o)
            #終點(diǎn)
            route.append(d)
            #起點(diǎn)坐標(biāo)
            ori=gain_location(o)
            #終點(diǎn)坐標(biāo)
            des=gain_location(d)
            #路線規(guī)劃
            info=get_route(ori,des)
            if info["info"]=='OK':
                #起終點(diǎn)步行距離
                try:
                    walk_distance=info['route']['distance']
                except:
                    walk_distance='null'
                route.append(walk_distance)
                # 路線出租車(chē)費(fèi)用
                try:
                    taxi_cost=info['route']['taxi_cost']
                except:
                    taxi_cost='null'
                route.append(taxi_cost)
                #路線時(shí)間
                try:
                    duration=info['route']['transits'][0]['duration']
                except:
                    duration='null'
                route.append(duration)
                #路線費(fèi)用
                try:
                    price=info['route']['transits'][0]['cost']
                except:
                    price='null'
                route.append(price)
                #路線距離
                try:
                    distance=info['route']['transits'][0]['distance']
                except:
                    distance='null'
                route.append(distance)
                print(o,d,taxi_cost,duration,price,distance)
                routeplan.append(route)
    return routeplan

4. 主函數(shù)

在主函數(shù)中設(shè)定起點(diǎn)和終點(diǎn),并調(diào)用返回信息函數(shù),就能得到每個(gè)起點(diǎn)到每個(gè)終點(diǎn)的步行距離,路線出租車(chē)費(fèi)用,路線時(shí)間,路線費(fèi)用,路線距離信息了。

if __name__ == '__main__':
    start=['金隅麗景園','蘇荷時(shí)代','天恒樂(lè)活城','明悅灣','gogo新世代']
    end=['新中關(guān)購(gòu)物中心','五道口購(gòu)物中心','天作國(guó)際大廈','朱辛莊地鐵站','朝陽(yáng)建外soho','海淀文教產(chǎn)業(yè)園']
    routeplan=get_route_info(start,end)
    print(routeplan)

3.效果

結(jié)果以列表的形式返回,結(jié)果如圖。

路徑規(guī)劃效果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容