我正在尝试编写Python代码以获取从A点到B点的路线。从这些路线中,我希望能够获得沿路线每小时的坐标。有没有人知道的资源或可以帮助我完成此任务的库?一切都会有所帮助。
首先,我尝试获取路线并提取持续时间:
import googlemaps
import pprint
gmaps = googlemaps.Client(key='API_KEY')
directions = gmaps.directions('3000 Enterprise Dr, Allen Park, MI 48101',
'233 S Wacker Dr, Chicago, IL 60606', mode="driving")
pprint.pprint(directions)
route = directions[0]['legs'][0]['steps']
pprint.pprint(route)
time = []
for step in route:
time_string = step['duration']['text']
duration = [int(x) for x in time_string.split() if x.isdigit()]
if len(duration) == 2:
minutes = duration[0] * 60 + duration[1]
print(minutes)
else:
if 'hour' in time_string:
minutes = duration[0] * 60
print(minutes)
else:
minutes = duration[0]
print(minutes)
输出:这些是每个步骤的持续时间(以分钟为单位)
1
1
1
204
1
1
20
9
5
1
1
1
3
1
2
1
1
1
1
1