根据一个中文的地址信息,获取该地址所对应的经纬度信息。(专业的说法是地理编码)。编程语言:Python3,百度地图API接口:http://lbsyun.baidu.com/index.php?title=webapi
获取地址的经纬度大致步骤如下:
- 1.注册百度账号、登录百度地图,申请秘钥:http://lbsyun.baidu.com/apiconsole/key?application=key
- 2.查看百度地图中关于地址解析的服务文档:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
- 3.Python 编写request请求,使用GET访问接口:http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
- 4.Python解析返回的json结果,得到经纬度信息
# encoding:utf-8
import requests
import time
# 此处需要ak,ak申请地址:https://lbs.amap.com/dev/key/app
ak = "xxxxxxxxxxx"
headers = {
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/56.0.2924.87 Safari/537.36',
'Referer': 'https://restapi.amap.com/'
}
# 地理信息解析
def amp_geocode(addr=None):
url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {"key": ak,
"address": addr}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
try:
loc_info = response.json()["geocodes"][0]["location"]
lng = loc_info.split(",")[0]
lat = loc_info.split(",")[1]
print(loc_info)
time.sleep(0.25)
return (lng, lat)
except Exception as e:
print("Exception in amp_geocode",e)
time.sleep(5)
return None
else:
print("========>", response.status_code)
time.sleep(5)
return None
注意事项: