方法也是非常的简单,首先去高德开发平台,先申请高德API,设定一个Web服务的Key,然后将你自己的secret key
填到下面的key
对应的地方,就ok啦~
import requests
def getcode(site,city):
parameters = {'address': site,'city':city, 'key': 'your secret key'}
base_url = 'https://restapi.amap.com/v3/geocode/geo'
response = requests.get(url=base_url, params=parameters)
info_site = response.json()
return info_site['geocodes'][0]['location']
# print(info_site['geocodes'][0]['location'])
def lo_to_addr(location):
parameters = {'location': location, 'key': 'your secret key'}
base_url = 'https://restapi.amap.com/v3/geocode/regeo'
response = requests.get(url=base_url, params=parameters)
info_site = response.json()
# return info_site
return info_site['regeocode']['formatted_address']
# print(info_site['regeocode']['formatted_address'])
if __name__ == '__main__':
address = '北京市朝阳区人民政府'
city = '北京市'
location = getcode(address,city)
address_from_location = lo_to_addr(location)
print('根据输入的地址获取到的经纬度为:',location)
print('根据经纬度得到的地址为:',address_from_location)