根据地址获取经纬度坐标
根据地址获取坐标经纬度
根据地址查经纬度坐标
目标:根据提供的一串地址的文字描述,查出此地址对应的经纬度。
例如你要查询『北京市西城区西长安街2号 』或『国家大剧院』的坐标。
当然你也可以直接运行本文的代码,我们会得到一个坐标
81.84627200861524, 43.2213079855618,把此坐标粘贴到坐标拾取网站:拾取坐标系统
你会看到一个八卦城。
话不多说,代码如下:
# 根据文字查坐标
import json
from urllib.request import urlopen, quote
import requests
# 定义一个函数
def getlnglat(address):
url = 'https://api.map.baidu.com/geocoding/v3/'
output = 'json'
ak = '你申请的ak' # 你申请的ak
address = quote(address) # 由于本文地址变量为中文,为防止乱码,先用quote进行编码
uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak +""
req = urlopen(uri)
res = req.read().decode()
print(res)
temp = json.loads(res)
result = temp['result']
lat = result['location']['lat']
lng = result['location']['lng']
print(result['precise'],result['confidence'],result['comprehension'],result['level'])
return (lat, lng),(lng,lat)
getlnglat('特克斯县的中心太极坛')