首先複習一下本系列第一篇我們用的程式碼:
import googlemaps | |
google_key = “xxx” | |
gmaps = googlemaps.Client(key = google_key) | |
radar_results = gmaps.places_radar(location = (25.034195, 121.564467), radius = 100, type = “cafe”) | |
print(radar_results) |
最後得到的 radar_results 是一個 dict,裡面有三個根元素,分別包含三個 dict。
我們要用到的是 [‘results’] 這個 key。
[‘results’] 這個 key 的 value 是一個 list,裡面有 12 個項目,代表 101 為中心,半徑 100 公尺內有 12 個被 Google 標記上 ‘cafe’ 的地點。
每一個項目又是一個 dict,而我們要取出其中的 [‘place_id’] 來做進一步的利用。
如果覺得上一句在繞口令的話,去看這一篇的圖片會比較容易懂。
接下來我們將取出的 place_id 當作參數,帶入函數中來取得地點 (店家) 的詳細資料;
radar_results = radar_results['results'] | |
for radar_result in radar_results: | |
place_id = radar_result['place_id'] | |
#以上取出12個地點的place_id,以下帶入函數place(place_id, language) | |
detail_results = gmaps.place(place_id, language = "zh-tw") | |
print(detail_results['result']['name']) |
- 說在前面,我喜歡用有加s跟沒加 s 來取名,所以請注意名字的不同。
- 取得 place_id 之後,帶入地點詳細資料函數。
- 其中 place_id 是必要參數,而 language 則是選擇參數。
- 最後一樣得到一個 json 結果回傳,這邊示範列印出店家名稱。
- 12 個店家的名稱如下圖
Python – 使用 Google Map API(3) – 取得地點詳細資料 有 “ 2 則迴響 ”