# [Python]利用高德地图api实现经纬度与地址的批量转换

1.申请key

2.坐标转换

def transform(location):
parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'}
base = 'http://restapi.amap.com/v3/assistant/coordinate/convert'
response = requests.get(base, parameters)
return answer['locations']

2.地理/逆地理编码

def geocode(location):
parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'}
base = 'http://restapi.amap.com/v3/geocode/regeo'
response = requests.get(base, parameters)
return answer['regeocode']['addressComponent']['district'].encode('gbk','replace'),answer['regeocode']['formatted_address'].encode('gbk','replace')

3.从文件中读取

def parse():
datas = []
totalListDict = totalListData.to_dict('index')
for i in range(0, len(totalListDict)):
datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy']))
return datas

4.完整代码

#!/usr/bin/env
#-*- coding:utf-8 -*-
'''

'''
import requests
import pandas as pd
import time
import sys
sys.setdefaultencoding("utf-8")

def parse():
datas = []
totalListDict = totalListData.to_dict('index')
for i in range(0, len(totalListDict)):
datas.append(str(totalListDict[i]['centroidx']) + ',' + str(totalListDict[i]['centroidy']))
return datas

def transform(location):
parameters = {'coordsys':'gps','locations': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'}
base = 'http://restapi.amap.com/v3/assistant/coordinate/convert'
response = requests.get(base, parameters)

def geocode(location):
parameters = {'location': location, 'key': '7ec25a9c6716bb26f0d25e9fdfa012b8'}
base = 'http://restapi.amap.com/v3/geocode/regeo'
response = requests.get(base, parameters)

if __name__=='__main__':
i = 0
count = 0
df = pd.DataFrame(columns=['location','detail'])
#locations = parse(item)
locations = parse()
for location in locations:
dist, detail = geocode(transform(location))
df.loc[i] = [dist, detail]
i = i + 1
df.to_csv('locdetail.csv', index =False)


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120