Python调用百度地图API实现路径规划提取真实距离、时间

您所在的位置:网站首页 路径规划失败怎么回事 Python调用百度地图API实现路径规划提取真实距离、时间

Python调用百度地图API实现路径规划提取真实距离、时间

2023-07-02 06:36| 来源: 网络整理| 查看: 265

1.注册百度地图开放平台账号

网址:百度地图开放平台 | 百度地图API SDK | 地图开发

2.打开控制台,创建应用、获取AK

如下图所示

 桥重点、敲重点、瞧重点

在使用python调用API端口时,我们需要申请的为服务端应用类别,别申请错了,否则可能会报错,如APP服务被禁用。

 3.Python程序

我们使用程序获取数据,本质时数据爬虫,这里由于数据比较简单,我们快速直接到代码环节了。

def get_info(start,end,ak): url = 'https://api.map.baidu.com/directionlite/v1/walking?origin=' + start + '&destination=' + end + '&ak=' + ak + '&coord_type=wgs84' response = requests.get(url) answer = response.json() if answer["status"] == 0: get_distance = answer['result']['routes'][0]['distance'] get_time = answer['result']['routes'][0]['duration'] else: get_distance =0 get_time=0 return get_distance,get_time

这里强调一下,start、end的数据格式。

        start:'29.6634,106.599993'

        end:'29.56521,106.581208'

这里能获取到了话,我相信后面的就都非常简单了,比如存储,保存。

# 保存数据 res_dis = [] res_time = [] length = len(data) print("合计有{}行".format(length)) for i in range(length): cur_data_coor = data['combin_coor'][i] print(cur_data_coor) print("//") print("现在在处理有{}行".format(i)) temp_dis = [] temp_time = [] for j in range(length): node_data_coor = data['combin_coor'][j] print(node_data_coor) if i==j: get_distance =0 get_time = 0 else: get_distance, get_time = get_info(cur_data_coor,node_data_coor,ak) print("起点",'\t',"终点",'\t',"距离") print(cur_data_coor,'\t',node_data_coor,'\t',get_distance) # 以千米为单位 temp_dis.append(get_distance/1000) temp_time.append(get_time) res_dis.append(temp_dis) res_time.append(temp_time) print(res_dis)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3