高德地图API相关功能简单展示
# -- coding: utf-8 --
import requests
import pandas as pd
key = '***************' #高德地图api申请的key
#查询位置(地理编码)
def geo(address:str,city=None) ->dict:
url="https://restapi.amap.com/v3/geocode/geo?parameters" #API服务地址
parameters={
'key':key,
'city':city,
'citylimit':True,
"address":address
}
r=requests.get(url,params=parameters)
data=r.json()['geocodes'][0]['location']
return data
location=geo("河北大学东门");print(location)
location=geo("河北大学西门");print(location)
#路径规划 walking
def walking(origin:str,destination:str)->dict:
print('start')
url='https://restapi.amap.com/v3/direction/walking?parameters'
parameters={
'key':key,
'origin':origin,
'destination':destination
}
r=requests.get(url,params=parameters)
data=r.json()['route']['paths'][0]['steps'] #把steps取出来,是列表形式,列表里有许多字典,每个字典的第一个键是instruction,
for i in range(0,len(data)): # 即从0开始索引到最后一个,并取每个字典第一个键对应的值
print(data[int(i)]['instruction'])
return 'end'
print(walking('114.527450,37.997567','114.517366,37.994022'))
#poi(关键字)
def text(city=None,Keywords='楼',types='190000')->dict: ##types及poi类型代码,具体代码见高德开发平台,也可私信我
parameters={
'key':key,
'types':types,
'Keywords':Keywords,
'city':city,
'output':'josn',
'offset':10000,
}
r=requests.get("https://restapi.amap.com/v3/place/text?parameters",params=parameters)
data=r.json()['pois']
cot=pd.json_normalize(data) #转为dataframe格式
Ntxt=open('file.txt','w',encoding='utf-8')
cot.to_json(Ntxt)
Ntxt.close()
Ncsv=open('file.csv','w',encoding='utf-8') #创建csv文件,可写、编码
cot.to_csv(Ncsv,sep=',',index=False,header=True) #dataframe to csv (转入的文件,逗号分隔,是否索引,是否设置标题)
Ncsv.close()
return data
text('0311') #这里的城市可选择使用中文全称,也可用城市代码,具体代码见高德开发平台,也可私信
部分结果展示