调用百度api通过经纬度获取实际地理位置信息以及行政编码
需要用到百度地图api地址编码(有地址转经纬度)和逆地址编码功能(由经纬度转标准地址省市区),去申请密钥进行调用。
申请密钥的时候不选择sn,可以避免后面的调用百度地图API报错'211' sn校验失败。
地址编码官方文档:地理编码 | 百度地图API SDK (baidu.com)
逆地址编码官方文档:全球逆地理编码 rgc 反geo检索 | 百度地图API SDK (baidu.com)
主要实现代码:
# 2.调用百度api实现提取地点的经纬度 def getJingWei(address): url = "https://api.map.baidu.com/geocoding/v3" params = { "address": address, "output": "json", "ak": "你自己的ak密钥", } response = requests.get(url=url, params=params) result = response.json() # print(result) longitude = result['result']['location']['lng'] # 经度 latitude = result['result']['location']['lat'] # 纬度 lnglist.append(longitude) latlist.append(latitude) # 3.通过经纬度获取地址信息(省市区) def getAddress(lng, lat): response = requests.get('https://api.map.baidu.com/reverse_geocoding/v3/?ak=你自己的ak密钥&output=json&coordtype=wgs84ll&location='+str(lat)+','+str(lng)) # if response: # print(response.json()) result = response.json() addlist.append(result['result']['formatted_address']) alllist.append(result['result']['addressComponent']['province']+','+result['result']['addressComponent']['city']+','+result['result']['addressComponent']['district']+','+result['result']['addressComponent']['adcode'])
print(list[0]) getJingWei(list[0]) print(lnglist[0],latlist[0]) getAddress(lnglist[0],latlist[0]) print(addlist[0]) print(alllist[0])
输出结果如下:
总实现代码,并且输出到指定的txt文件中:
# -*- coding: utf-8 -*- """ 给地址添加行政编码 """ import codecs import requests import numpy as np """ 抽取地址,调用接口查看该地址属于哪个省市区 """ # 1.获取需要的地址数据 # def splittxt(): f = codecs.open('test.txt', mode='r', encoding='utf-8') lines = f.readlines() list = [] lnglist = [] # 经度 latlist = [] # 维度 addlist = [] # 标准地址格式省市区 alllist=[] # 将标准地址省市区组合在一起 for line in lines: # print(line.strip().split("\",")[15][1:]) #测试 list.append(line.strip().split("\",")[15][1:]) f.close() # print(list[0]) # 2.调用百度api实现提取地点的经纬度 def getJingWei(address): url = "https://api.map.baidu.com/geocoding/v3" params = { "address": address, "output": "json", "ak": "你自己的ak密钥", } response = requests.get(url=url, params=params) result = response.json() # print(result) longitude = result['result']['location']['lng'] # 经度 latitude = result['result']['location']['lat'] # 纬度 lnglist.append(longitude) latlist.append(latitude) # 3.通过经纬度获取地址信息(省市区) def getAddress(lng, lat): response = requests.get('https://api.map.baidu.com/reverse_geocoding/v3/?ak=你自己的ak密钥&output=json&coordtype=wgs84ll&location='+str(lat)+','+str(lng)) # if response: # print(response.json()) result = response.json() addlist.append(result['result']['formatted_address']) alllist.append(result['result']['addressComponent']['province']+','+result['result']['addressComponent']['city']+','+result['result']['addressComponent']['district']+','+result['result']['addressComponent']['adcode']) # 测试 for i in range(len(list)): print(list[i]) getJingWei(list[i]) print(lnglist[i],latlist[i]) getAddress(lnglist[i],latlist[i]) print(addlist[i]) print(alllist[i]) # 将结果输出到指定文件中 # 标准形式地址 res_all = '\n'.join(addlist) with open('addlistrs.txt', 'w', encoding="utf-8") as f: f.write(res_all) # 标准地址的省市区 res_all = '\n'.join(alllist) with open('alllistrs.txt', 'w', encoding="utf-8") as f: f.write(res_all)