根據一個中文的地址信息,獲取該地址所對應的經緯度信息。(專業的說法是地理編碼)。編程語言: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
注意事項:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
