我想通过谷歌方向API动态查询谷歌地图。例如,这个请求计算了从伊利诺伊州芝加哥到加利福尼亚州洛杉矶的路线,途经密苏里州乔普林和俄克拉荷马城的两个路点,OK:
http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
它返回JSON格式的结果。
如何在Python中做到这一点?我想发送这样一个请求,接收结果并解析它。
我推荐使用很棒的请求库:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON响应内容:https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content