网址:百度地图开放平台 | 百度地图API SDK | 地图开发
精简版程序见后文
import requests
import json
ak = '复制上文中的AK'
origin = '40.01116,116.339303' #起始点经纬度坐标
destination = '39.936404,116.452562' #终点经纬度坐标
url = 'https://api.map.baidu.com/directionlite/v1/walking?origin='\
+ origin + '&destination=' + destination +'&ak=' + ak + '&coord_type=wgs84'
'''
url数据解释
1. walking是步行导航的意思,如果想要其他导航方式可参考官方文档
riding是骑行导航
driving是驾车导航
transit是公交导航
等等
2. origin是起始点的经纬度坐标,前者为纬度,后者为经度
3. destination是终点的经纬度坐标
4. ak是就是上文中的ak
5. coord_type=wgs84的意思是gps坐标。因为默认是百度经纬度坐标。所有格式见下图
'''
response = requests.get(url) #请求连接
answer = response.json() #获取json格式
answer_json= json.dumps(answer, indent=4) #增加缩进,不然可能得到的数据都在一行
print(answer_json)#打印查看
coord_type 是坐标的格式。见代码的第8行末尾和第14行
精简版程序
import requests
import json
ak = '复制上文中的AK'
origin = '40.01116,116.339303'
destination = '39.936404,116.452562'
url = 'https://api.map.baidu.com/directionlite/v1/walking?origin='\
+ origin + '&destination=' + destination +'&ak=' + ak + '&coord_type=wgs84'
response = requests.get(url)
answer = response.json()
answer_json= json.dumps(answer, indent=4)
print(answer_json)
import requests
ak = '你的ak'
origin = '40.01116,116.339303'
destination = '39.936404,116.452562'
url = 'https://api.map.baidu.com/directionlite/v1/walking?origin='\
+ origin + '&destination=' + destination +'&ak=' + ak + '&coord_type=wgs84'
response = requests.get(url)
answer = response.json()
'''
以上步骤获取了路径中的各种信息,下面我们只保留gps经纬度坐标
'''
steps = answer['result']['routes'][0]['steps']
'''
根据官网的文件格式可知,经纬度路线保存在了answer['result']['routes'][0]['steps']里的path中
!注意!
1. result是json格式,routes是数组格式,所以routes后要加一个[0]表示第一条路径,steps也是数组格式
2. path是字符串格式,将其转换成数组格式,用分号隔开
'''
global_path = []#保存全局路径
for path_i in steps:
path = path_i['path'].split(';')
#path是字符串格式,将其转换成数组格式,用分号隔开
global_path.extend(path)
#将每一个path里的路径都添加到这个大数组里面
global_path_str = '\n'.join(str(i) for i in global_path)
#因为写入文件的时候只能写入字符串,所以将数组转化成字符串,相邻的经纬度组合用换行符分割开
filename = 'direction_routes.txt'
with open(filename, 'w+')as file:
file.write(global_path_str)
从百度地图获取的路径规划的详细信息,json格式
{
"status": 0,
"message": "ok",
"result": {
"origin": {
"lng": 116.3520959054,
"lat": 40.018168418037
},
"destination": {
"lng": 116.46533629799,
"lat": 39.94339942387
},
"routes": [
{
"distance": 16959,
"duration": 14522,
"steps": [
{
"distance": 117,
"duration": 100,
"direction": 9,
"instruction": "\u4ece\u8d77\u70b9\u5411\u6b63\u897f\u65b9\u5411\u51fa\u53d1,\u8d70120\u7c73,\u5411\u53f3\u524d\u65b9\u8f6c",
"path": "116.35202044774,40.018115233459;116.35104309134,40.0180861546;116.35100805743,40.018061979767;116.35098020996,40.018035111157;116.35093170146,40.017806416723",
"start_location": {
"lng": "116.35202044774",
"lat": "40.018115233459"
},
"end_location": {
"lng": "116.35093170146",
"lat": "40.017806416723"
}
},
{
"distance": 33,
"duration": 28,
"direction": 9,
"instruction": "\u8d7030\u7c73,\u53f3\u8f6c\u8fdb\u5165\u53cc\u6e05\u8def",
"path": "116.35093170146,40.017806416723;116.35078976919,40.017809870284;116.35054183687,40.017788527273",
"start_location": {
"lng": "116.35093170146",
"lat": "40.017806416723"
},
"end_location": {
"lng": "116.35054183687",
"lat": "40.017788527273"
}
},
# *******************************************************************
# *********************由于篇幅中间省略了许多*************************
# *******************************************************************
{
"distance": 130,
"duration": 111,
"direction": 3,
"instruction": "\u6cbf\u4e09\u91cc\u5c6f\u4e1c\u4e09\u8857\u8d70130\u7c73,\u53f3\u8f6c",
"path": "116.46205478796,39.943689149515;116.46218324565,39.943685692176;116.46221558465,39.943685000708;116.46357382258,39.943660107859",
"start_location": {
"lng": "116.46205478796",
"lat": "39.943689149515"
},
"end_location": {
"lng": "116.46357382258",
"lat": "39.943660107859"
}
},
{
"distance": 11,
"duration": 9,
"direction": 6,
"instruction": "\u8d7010\u7c73,\u5de6\u8f6c",
"path": "116.46357382258,39.943660107859;116.46357921241,39.943610322135;116.46359089038,39.94356399372",
"start_location": {
"lng": "116.46357382258",
"lat": "39.943660107859"
},
"end_location": {
"lng": "116.46359089038",
"lat": "39.94356399372"
}
},
{
"distance": 149,
"duration": 127,
"direction": 3,
"instruction": "\u8d70150\u7c73,\u5230\u8fbe\u7ec8\u70b9",
"path": "116.46359089038,39.94356399372;116.4645233315,39.943548712251;116.46479282315,39.943537026419;116.46489702659,39.943532877602;116.46500302664,39.943527968169;116.46535067088,39.943512824986",
"start_location": {
"lng": "116.46359089038",
"lat": "39.94356399372"
},
"end_location": {
"lng": "116.46535067088",
"lat": "39.943512824986"
}
}
]
}
]
}
}
精简版程序
import requests
ak = '你的ak'
origin = '40.01116,116.339303'
destination = '39.936404,116.452562'
url = 'https://api.map.baidu.com/directionlite/v1/walking?origin='\
+ origin + '&destination=' + destination +'&ak=' + ak + '&coord_type=wgs84'
response = requests.get(url)
answer = response.json()
steps = answer['result']['routes'][0]['steps']
global_path = []
for path_i in steps:
path = path_i['path'].split(';')
global_path.extend(path)
global_path_str = '\n'.join(str(i) for i in global_path)
filename = 'direction_routes.txt'
with open(filename, 'w+')as file:
file.write(global_path_str)
如有不足,请各位前辈指教。