国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:6日期:2022-06-16 08:24:55
目錄一、簡(jiǎn)介思路高德地圖API二、獲取經(jīng)緯度三、路線規(guī)劃(四種方式)獲取出行路線數(shù)據(jù)處理四、演示效果五、結(jié)尾一、簡(jiǎn)介

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

思路 根據(jù)地點(diǎn)獲取經(jīng)緯度 根據(jù)經(jīng)緯度調(diào)用api獲取路線 對(duì)路線數(shù)據(jù)進(jìn)行處理,便于瀏覽高德地圖API

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

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

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

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

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

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

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

def get_location_x_y(place): #place = input('請(qǐng)輸入您要查詢的地址') 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é)果

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

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

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

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

type是出行方式(四種方式對(duì)應(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'

請(qǐng)求參數(shù)

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

參數(shù)中from_location是起點(diǎn)經(jīng)緯度,to_location是終點(diǎn)經(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不一樣,所以需要對(duì)應(yīng)的去處理,便于瀏覽。

四、演示效果

1、公交

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

2、步行

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

3、駕車

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

4、騎行

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

五、結(jié)尾

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 曲沃县| 色达县| 盖州市| 常州市| 天镇县| 高平市| 定南县| 拜泉县| 余姚市| 巧家县| 德格县| 辽阳市| 曲水县| 沿河| 东乌| 定西市| 襄樊市| 涿州市| 尚义县| 山西省| 永年县| 琼中| 大厂| 民乐县| 碌曲县| 西充县| 长乐市| 龙胜| 德庆县| 木兰县| 石泉县| 梅河口市| 布尔津县| 宾川县| 浏阳市| 定兴县| 建阳市| 宜兴市| 衡南县| 阳谷县| 漳平市|