需求:用经纬度的值,获取省市区街道信息。
因其经纬度是 高德的,所以需要用到高德地图的api。
实现方法/步骤:
1、把经纬度数据,存放在excel表格里面。
2、用python去读取数据,然后执行高德api接口。
3、把接口返回的数据,写入到excel表格里面。
初始经纬度数据:
import requestsfrom openpyxl import load_workbookworkbook = load_workbook(filename = "location.xlsx")workbook.sheetnamessheet = workbook["Sheet1"]sheet2 = workbook["Sheet2"]listString = []listArr = []# 获取表格所有行for val in sheet.rows: for i in val: listString.append(i.value)# 按2个拆分成数组def list_split(items, n): return [items[i:i+n] for i in range(0, len(items), n)]if '__main__' == __name__: listArr = list_split(listString, 2)# print(listString)# print(listArr)# 高德地图的KEY,和location 参数,经度在前,维度在后data = { 'key': 'ab8***************************148', 'location': '',}for val in listArr[1:len(listArr)]: # 先判断excel的数据是不是空的 if val[0]!=None or val[1] != None: # 添加经度和维度的值 data['location'] = str(val[0]) + ',' + str(val[1]) answer = [] url = "http://restapi.amap.com/v3/geocode/regeo" response = requests.get(url,data) answer.append(response.json()) # print(answer) for valInfo in answer: province = valInfo ['regeocode']['addressComponent']['province'] city = valInfo ['regeocode']['addressComponent']['city'] district = valInfo ['regeocode']['addressComponent']['district'] township = valInfo ['regeocode']['addressComponent']['township'] street = valInfo ['regeocode']['addressComponent']['streetNumber']['street'] # 街道数据为[]时,返回'',不然会报错 if len(street)==0: street = '' line = [val[0],val[1],province,city,district,township,street] # print(line) sheet2.append(line)workbook.save(filename = "location.xlsx")
执行后结果数据:
反之:用省市区获取经纬度也是差不多的,只不过是api接口不同而已。