第一步:申请key
第二步:发起http/https请求
第三步:接收请求返回的数据(JSON或XML格式)
url:https://restapi.amap.com/v3/geocode/geo?parameters
https://restapi.amap.com/v3/geocode/geo?address=北京市朝阳区阜通东大街6号&output=XML&key=<用户的key>
返回的参数通过请求参数 output 指定,默认为 JSON 形式。返回的省市区信息为简要地址信息
def address(address):
url="http://restapi.amap.com/v3/geocode/geo?key=%s&address=%s"%('你的key',address)
data=requests.get(url)
contest=data.json()
#返回经度和纬度
contest=contest['geocodes'][0]['location']
return contest
批量地址编码
def geocode_batch(address_list):
location_list = []
num_reqs = len(address_list) // 20
for index in range(num_reqs):
sl = slice(index * 20, (index + 1) * 20)#每20个进行解析
address_picked = address_list[sl]
address = '|'.join(address_picked) # 用“|”拼接地址
url = 'http://restapi.amap.com/v3/geocode/geo'
params = {
'address':address,
'key':'你的key',
'batch':True # 要传batch参数,False为单点查询
}
try:
res = requests.get(url, params, timeout=10)
for add, geo in zip(address_picked, res.json()['geocodes']):
if geo['location']: # 当地址错误时,该地址的location为空
location = map(float, geo['location'].split(','))
else:
print('address:{} can\'t be geocoded.'.format(add))
location = [None] * 2 # 异常值用None代替
location_list.append(location)
except Exception as e:
print(e)
location_list += [[None, None]] * len(address_picked)
return location_list
不同写法:
import requests
# 执行一次高德地图地理逆编码的查询
# 输入值:coordList -> 经纬度的序列,currentKey -> 当前使用的Key
# 返回值:resultList -> 查询成功,返回结果地址的序列
# -1 -> 执行当前查询时Key的配额用完了
# -2 -> 执行当前查询出错
def geocode(locationlist):
res=[]
for add in locationlist:
url = 'https://restapi.amap.com/v3/geocode/regeo?key=你的key&location='+add
# print(url)
response = requests.get(url=url)
answer = json.loads(response.text)
adress = answer['regeocode']['formatted_address']
# adress = answer['regeocode']['addressComponent']['province'] + \
# answer['regeocode']['addressComponent']['city'] + \
# answer['regeocode']['addressComponent']['district']+\
# answer['regeocode']['addressComponent']['township']
res.append(adress)
return res
driving 可以换成walking
https://restapi.amap.com/v3/direction/driving?origin=116.45925,39.910031&destination=116.587922,40.081577&output=xml&key=<用户的key>
def get_route(origin,destination):
key = '你的key'
url = https://restapi.amap.com/v3/direction/driving?origin={origin}&destination={destination}&output=xml&key=<你的key>
res = request.get(url)
res = res.text
jsonData=json.loads(res)
return jsonData
def get_route_info(start,end):
routeplan=[]
for o in start:
for d in end:
route=[]
#起点
route.append(o)
#终点
route.append(d)
#起点终点转换为经纬度
ori=address(o)
des=address(d)
#路线规划
info=get_route(ori,des)
#status为0时,info返回错误原;否则返回“OK”。详情参阅info状态表
if info["info"]=='OK':
#起终点步行距离
try:
walk_distance=info['route']['distance']
except:
walk_distance='null'
route.append(walk_distance)
# 路线出租车费用
try:
taxi_cost=info['route']['taxi_cost']
except:
taxi_cost='null'
route.append(taxi_cost)
#路线时间
try:
duration=info['route']['transits'][0]['duration']
except:
duration='null'
route.append(duration)
#路线费用
try:
price=info['route']['transits'][0]['cost']
except:
price='null'
route.append(price)
#路线距离
try:
distance=info['route']['transits'][0]['distance']
except:
distance='null'
route.append(distance)
print(o,d,taxi_cost,duration,price,distance)
routeplan.append(route)
return routeplan
#关键字搜索
https://restapi.amap.com/v3/place/text?keywords=北京大学&city=beijing&output=xml&offset=20&page=1&key=<用户的key>&extensions=all
#周边搜索
https://restapi.amap.com/v3/place/around?key=<用户的key>&location=116.473168,39.993015&radius=10000&types=011100
#多边形搜索
https://restapi.amap.com/v3/place/polygon?polygon=116.460988,40.006919|116.48231,40.007381|116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919&keywords=kfc&output=xml&key=<用户的key>
#id查询
https://restapi.amap.com/v3/place/detail?id=B0FFFAB6J2&output=xml&key=<用户的key>
def PolygonSearch(address_list):
address = '|'.join(address_list) # 用“|”拼接地址
inputUrl = "https://restapi.amap.com/v3/place/polygon?polygon=" + address + "&offset=20&page=1&types=010000&output=json&key=0ff30ea4c08544b5df4ed773a5a6636f"
response = requests.get(inputUrl)
# 返回结果,JSON格式
resultAnswer = response.json()
# 返回结果中的状态标签,1为成功,0为失败
resultStatus = resultAnswer['status']
if resultStatus == '1': # 返回成功
# 读取返回的POI列表
resultList = resultAnswer['pois']
if len(resultList) == 0: # 返回的POI列表为空
print("当前返回结果为空!")
else:
# 返回的POI列表不为空,则遍历读取所有的数据
for j in range(0, len(resultList)):
saveId = str(resultList[j]['id']) # POI编号
saveName = str(resultList[j]['name']) # POI名称
saveType = str(resultList[j]['type']) # POI类别
saveTypecode = str(resultList[j]['typecode']) # POI类别编号
saveAddress = str(resultList[j]['address']) # POI地址
saveLocation = str(resultList[j]['location']) # POI坐标
print([saveId, saveName, saveType, saveTypecode, saveAddress, saveLocation])
else:
print("当前返回结果错误!")
url = "https://restapi.amap.com/v3/weather/weatherInfo"
key = '你的key'
data = {'key': key, "city": 320100}
req = requests.post(url, data)
info = req.json()