主頁 > 知識庫 > python四種出行路線規(guī)劃的實現(xiàn)

python四種出行路線規(guī)劃的實現(xiàn)

熱門標簽:服務(wù)器配置 呼叫中心市場需求 鐵路電話系統(tǒng) 網(wǎng)站文章發(fā)布 檢查注冊表項 美圖手機 智能手機 銀行業(yè)務(wù)

一、簡介

路徑規(guī)劃中包括步行、公交、駕車、騎行等不同方式,今天借助高德地圖web服務(wù)api,實現(xiàn)出行路線規(guī)劃。

思路

  • 根據(jù)地點獲取經(jīng)緯度
  • 根據(jù)經(jīng)緯度調(diào)用api獲取路線
  • 對路線數(shù)據(jù)進行處理,便于瀏覽

高德地圖API

對應(yīng)鏈接
https://lbs.amap.com/api/webservice/guide/api/direction

去高德地圖的開放平臺注冊一個賬號,并且創(chuàng)建自己的項目,系統(tǒng)會分配給你一個 key 值。

在開發(fā)支持中選擇 web服務(wù),選中 web服務(wù)api

二、獲取經(jīng)緯度

輸入地點、輸出經(jīng)緯度

def get_location_x_y(place):
    #place = input("請輸入您要查詢的地址")
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    parameters = {
        'key':'高德官網(wǎng)獲取key',
        'address':'%s' % place
    }
    page_resource = requests.get(url,params=parameters)
    text = page_resource.text       #獲得數(shù)據(jù)是json格式
    data = json.loads(text)         #把數(shù)據(jù)變成字典格式
    location = data["geocodes"][0]['location']
    return location
 
if __name__ == '__main__':
    print(get_location_x_y("北京西站"))

獲取結(jié)果

三、路線規(guī)劃(四種方式)

獲取起點、終點經(jīng)緯度、出行方式

from_place = input("請輸入起始地址")
from_location = get_location_x_y(from_place)
to_place = input("請輸入目的地")
to_location = get_location_x_y(to_place)
type = input("出行方式(1.公交、2.步行、3.駕車、4.騎行),請輸入數(shù)字")

獲取出行路線

type是出行方式(四種方式對應(yīng)1、2、3、4)
不同的出行方式,高德地圖web服務(wù)api鏈接也不同

url="https://restapi.amap.com"
if type=="1":
    url = url+ "/v3/direction/transit/integrated"
elif type=="2":
    url = url + "/v3/direction/walking"
elif type=="3":
    url = url + "/v3/direction/driving"
elif type == "4":
    url = url + "/v4/direction/bicycling"

請求參數(shù)

parameters = {
    'key': '高德官網(wǎng)獲取key',
    'origin': str(from_location),
    'destination': str(to_location),
    'extensions':'all',
    'output':'json',
    'city':'020',
}

參數(shù)中from_location是起點經(jīng)緯度,to_location是終點經(jīng)緯度,output是數(shù)據(jù)返回的格式,這里返回json(官網(wǎng)還給了很多種格式,比如xml等)

數(shù)據(jù)處理

if type=="1":
    txt = txt['route']['transits']
    for i in txt:
        i = i['segments'][0]['bus']['buslines'][0]['name']
        print(i)
elif type=="2":
    txt = txt['route']['paths'][0]['steps']
    for i in txt:
        i = i['instruction']
        print(i)
elif type=="3":
    txt = txt['route']['paths'][0]['steps']
    for i in txt:
        i = i['instruction']
        print(i)
elif type == "4":
    txt = txt['data']['paths'][0]['steps']
    for i in txt:
        i = i['instruction']
        print(i)

根據(jù)不同的出行方式,獲取的數(shù)據(jù)key不一樣,所以需要對應(yīng)的去處理,便于瀏覽。

四、演示效果

1、公交

2、步行

3、駕車

4、騎行

五、結(jié)尾

OK,以上就是python通過借助高德地圖web服務(wù)實現(xiàn)不同出行方式的路線規(guī)劃。

到此這篇關(guān)于python四種出行路線規(guī)劃的實現(xiàn) 的文章就介紹到這了,更多相關(guān)python 出行路線規(guī)劃 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python在openstreetmap地圖上繪制路線圖的實現(xiàn)
  • python全棧要學(xué)什么 python全棧學(xué)習(xí)路線

標簽:樂山 長治 沈陽 滄州 紅河 新疆 河南 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標題《python四種出行路線規(guī)劃的實現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266