[英]Extract information from nested dictionary (google maps distance_matrix)
[英]How to return just distance from a google maps distance matrix
我想使用谷歌地圖距離矩陣 API 確定兩個地址之間的距離。 如何編輯我的代碼,使我得到的 output 只是距離而不是整個 JSON 字典?
我目前正在使用 json ,它返回目的地、原點、行( distance(text, value)
和duration(text, value)
)和狀態。 我可以把它歸結為只顯示“行”但想隔離distance(value)
。 pick[i]
和drop[i]
是來自 csv 文件的輸入。
for i in range(1,10):
source = pick[i]
dest = drop[i]
url ='https://maps.googleapis.com/maps/api/distancematrix/json?'
r = requests.get(url + 'origins=' + source +
'&destinations=' + dest +
'&key=myapi')
x = r.json()
print(i,x['rows'],"\n")
我目前的 output 是:
[{'elements': [{'distance': {'text': '18.2 km', 'value': 18218}, 'duration': {'text': '21 mins', 'value': 1264}, 'status': 'OK'}]}]
此功能僅指定 Google 地圖距離矩陣 API 返回的特定字段目前不可用。 您將需要解析距離矩陣 API JSON 響應以獲取或顯示您需要的值。 您應該能夠通過以下方式訪問數據: x["elements"][0]["duration"]["value"]
此外,如果您有興趣擁有此功能,您可以在 Google 的公共問題跟蹤器中將其作為功能請求提交: https://developers.google.com/maps/support/#issue_tracker
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.