python四种出行路线规划的实现

路径规划中包括步行、公交、驾车、骑行等不同方式,今天借助高德地图web服务api,实现出行路线规划。感兴趣的可以了解下

一、简介

路径规划中包括步行、公交、驾车、骑行等不同方式,今天借助高德地图web服务api,实现出行路线规划。

思路

  • 根据地点获取经纬度
  • 根据经纬度调用api获取路线
  • 对路线数据进行处理,便于浏览

高德地图API

对应链接
https://lbs.amap.com/api/webservice/guide/api/direction

去高德地图的开放平台注册一个账号,并且创建自己的项目,系统会分配给你一个 key 值。

在开发支持中选择 web服务,选中 web服务api

二、获取经纬度

输入地点、输出经纬度

 def get_location_x_y(place): #place = input("请输入您要查询的地址") url = 'https://restapi.amap.com/v3/geocode/geo?parameters' parameters = { 'key':'高德官网获取key', 'address':'%s' % place } page_resource = requests.get(url,params=parameters) text = page_resource.text       #获得数据是json格式 data = json.loads(text)         #把数据变成字典格式 location = data["geocodes"][0]['location'] return location if __name__ == '__main__': print(get_location_x_y("北京西站")) 

获取结果

三、路线规划(四种方式)

获取起点、终点经纬度、出行方式

 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.骑行),请输入数字") 

获取出行路线

type是出行方式(四种方式对应1、2、3、4)
不同的出行方式,高德地图web服务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" 

请求参数

 parameters = { 'key': '高德官网获取key', 'origin': str(from_location), 'destination': str(to_location), 'extensions':'all', 'output':'json', 'city':'020', } 

参数中from_location是起点经纬度,to_location是终点经纬度,output是数据返回的格式,这里返回json(官网还给了很多种格式,比如xml等)

数据处理

 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) 

根据不同的出行方式,获取的数据key不一样,所以需要对应的去处理,便于浏览。

四、演示效果

1、公交

2、步行

3、驾车

4、骑行

五、结尾

OK,以上就是python通过借助高德地图web服务实现不同出行方式的路线规划。

到此这篇关于python四种出行路线规划的实现 的文章就介绍到这了,更多相关python 出行路线规划 内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是python四种出行路线规划的实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python