資源簡介
介紹了如何在百度地圖API申請密鑰,然后在Python中調用API接口將自有數據中的地名轉換為經緯度坐標。具體介紹可參照我的博客。
代碼片段和文件信息
from?urllib.request?import?urlopen?quote
import?requests
import?json
def?getlnglat(address):
“““根據傳入地名參數獲取經緯度“““
url?=?‘http://api.map.baidu.com/geocoder/v2/‘
output?=?‘json‘#輸出數據的格式
ak?=?‘申請的密鑰‘
add?=?quote(address)?#由于本文地址變量為中文,為防止亂碼,先用quote進行編碼
uri?=?url?+?‘?‘?+?‘address=‘?+?add??+?‘&output=‘?+?output?+?‘&ak=‘?+?ak?
req?=?urlopen(uri)
res?=?req.read().decode()?
temp?=?json.loads(res)
lat=temp[‘result‘][‘location‘][‘lat‘]
lng=temp[‘result‘][‘location‘][‘lng‘]
return?latlng
def?__jsonDump(name_json):
- 上一篇:python基礎培訓.ppt
- 下一篇:Python基礎教程pdf(第2版 修訂版)
評論
共有 條評論