调用高德
# 高德地图根据经纬度反查地址,每天只能调用5000次
def gaode_excute_single_query(coordStrings ,currentkey='你自己的api-key'):
# 1-将coordList中的经纬度坐标连接成高德地图API能够识别的样子
# coordString+= f"{long},{lat}|"
# 2-地理编码查询需要的Url
output = 'json' # 查询返回的形式
batch = 'true' # 是否支持多个查询
base = 'https://restapi.amap.com/v3/geocode/regeo?' # 逆地理编码查询Url的头
currentUrl = base + "output=" + output + "&batch=" + batch + "&location=" + coordStrings+ "&key=" + currentkey
# 3-提交请求
response = requests.get(currentUrl) # 提交请求
answer = response.json() # 接收返回
# 4-解析Json的内容
resultList = [] # 用来存放逆地理编码结果的空序列
if answer['status'] == '1' and answer['info'] == 'OK':
# 4.1-请求和返回都成功,则进行解析
tmpList = answer['regeocodes'] # 获取所有结果坐标点
for i in range(0,len(tmpList)):
try:
# 解析','分隔的经纬度
addressString = tmpList[i]['formatted_address']
# 放入结果序列
resp_dict=dict()
resp_dict[coordStrings]=addressString
resultList.append(resp_dict)
except:
# 如果发生错误则存入None
traceback.print_exc()
resultList.append(None)
return resultList
elif answer['info'] == 'DAILY_QUERY_OVER_LIMIT':
# 4.2-当前账号的余额用完了,返回-1
return -1
else:
# 4.3-如果发生其他错误则返回-2
return -2
调用百度
# 百度地图根据经纬度反查地址,每天只能调用5000次
def baidu_excute_single_query(coordStrings,ak='你自己的api-key'):
# coordStrings:格式 纬度,经度 f'{lat},{long}'
url=f'https://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={coordStrings}&'
resp=requests.get(url=url,params=None).json()
# print(resp)
if resp.get('result') :
res_dict=dict()
res_dict[coordStrings]=resp['result']['formatted_address']
return res_dict
return f"{coordStrings},解析失败"
标签:coordStrings,return,经纬度,python,resp,api,dict,高德
From: https://www.cnblogs.com/qtclm/p/16997747.html