您现在的位置是:首页 > 技术教程 正文

Python调用高德API进行多路径点规划,并输出地图

admin 阅读: 2024-03-26
后台-插件-广告管理-内容页头部广告(手机)

前言

参比赛时,需要调用高德API进行多路径点规划,结果高德一次性只能添加16个路径点,为了让多达20个路径点出现在一张地图上便写了这个代码

准备 :
需要一个高德官网申请的API Key
代码需要输入经纬度(这里就不过多讲述如何获取路径点的经纬度)
需要按照特定格式做成excel表格,相信对大家来说不是很难的

1.申请高德API

 先进入高德开放平台的控制台申请一个Key
 1.先创建一个应用
在这里插入图片描述
 2.选择添加key
在这里插入图片描述
 3.选择Web服务
在这里插入图片描述
 这就是我们需要的Key啦
在这里插入图片描述

2.代码部分

import folium from folium import plugins import numpy as np import pandas as pd import requests strategy = 5 #规划策略 见https://lbs.amap.com/api/webservice/guide/api/direction#driving gaode_key = '替换为你的Key' #调用高德地图key lat_and_lon = [] #储存详细坐标经纬度 Lon = [] # 储存经度 Lat = [] # 储存纬度 df = pd.read_excel('总数据.xlsx', sheet_name='一区') way_lon = df['经度'].tolist() way_lat = df['纬度'].tolist() waypoint = '' #路径点 jwd = df['经纬度'].tolist() #供应商经纬度 grouped_strings = [] tolls = 0 duration = 0 def get_routes(start, end, mode, gaode_key, waypoint): url = f'https://restapi.amap.com/v3/direction/driving?origin={start}&destination={end}&strategy={mode}&waypoints={waypoint}&key={gaode_key}' response = requests.get(url) data = response.json() if data['status'] == '1': routes = data['route']['paths'][0]['steps'] return routes else: print('未获取到相关路径') return None for i in range(0, len(jwd), 16): group = jwd[i:i + 16] string = ';'.join(map(str, group)) start = group[0] end = group[-1] waypoint = string # 获取路线规划 routes = get_routes(start, end, strategy, gaode_key, waypoint) if routes: for i, step in enumerate(routes): lat_and_lon.append(step["polyline"]) tolls += int(step["tolls"]) duration += int(step["duration"]) else: print('无法获取路线规划。') # 提取坐标点并全添加到 Lon 和 Lat 列表中 for item in lat_and_lon: points = item.split(';') for point in points: coords = point.split(',') Lon.append(float(coords[0])) Lat.append(float(coords[1])) def Map(Lat, Lon, la, lo): tri = np.array(list(zip(Lat, Lon))) san_map = folium.Map( location=[23.243466, 113.637713], zoom_start=16, # 调用高德街道图 tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}', # tiles='http://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}', # 高德卫星图 attr='default') folium.PolyLine(tri, color='#ff0000').add_to(san_map) marker_cluster = plugins.MarkerCluster().add_to(san_map) for lat, lon in zip(la, lo): icon = folium.Icon(color='red', icon='info-sign') # 自定义图标 folium.Marker([lat, lon], icon=icon).add_to(marker_cluster) san_map.save('地图.html') print("总花费{}元".format(tolls)) print("耗费{}分钟".format(duration/60)) def main(): Map(Lat, Lon, way_lat, way_lon) if __name__ == '__main__': main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

3.文件格式

 数据读取格式需要如下EXCEL表格格式
在这里插入图片描述
 获取经纬度也是有其余方法的就麻烦大家自己找一下,这里不过多阐述
 默认第一行为起点,最后一行为终点,中间的为途径点

4.输出结果

 输出的结果保存在代码目录的【地图.html】文件中
 直接用浏览器打开就行

5.结果展示

在这里插入图片描述
在这里插入图片描述

 有卫星地图和街道地图2种模式,这里仅展示一种

最后

 还有可优化的余地,请大家自便
 关于strategy路线选取策略,可移步高德的官方文档查看

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索
排行榜