python实现四种出行路线规划(公交、步行、驾车、骑行) 您所在的位置:网站首页 如何设计公交线路图 python实现四种出行路线规划(公交、步行、驾车、骑行)

python实现四种出行路线规划(公交、步行、驾车、骑行)

2024-07-16 04:53| 来源: 网络整理| 查看: 265

一、简介

路径规划中包括步行、公交、驾车、骑行等不同方式,今天借助高德地图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服务实现不同出行方式的路线规划。完整源码获取方式在下方

【本文完整源码获取方式】

公众号回复:高德出行

------------------- End -------------------

【各种爬虫源码获取方式】

识别文末二维码,回复:爬虫源码

35行代码下载任意网页的图片

python窃取摄像头照片(摄像头拍照+邮箱发送+打包exe)

30行爬虫代码实现中英互译

欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持

想加入Python学习群请在后台回复【入群】

万水千山总是情,点个【在看】行不行

 

【加群获取学习资料QQ群:901381280】 【各种爬虫源码获取方式】 识别文末二维码,回复:爬虫源码 欢迎关注公众号:Python爬虫数据分析挖掘,方便及时阅读最新文章 回复【开源源码】免费获取更多开源项目源码

ÃÃÃæÃÃÃìÃÃÃâÃÃÃèÃÃÃÿÃÃÃæÃÃÃëÃÃÃçÃÃÃà



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有