· 6 years ago · Sep 28, 2019, 09:42 AM
1import urllib.request, json
2def routing():
3
4 #Google MapsDdirections API endpoint
5 endpoint = 'https://maps.googleapis.com/maps/api/directions/json?'
6 api_key = '<replace key here>'
7 #Asks the user to input Where they are and where they want to go.
8 origin = input('Where are you?: ').replace(' ','+')
9 destination = input('Where do you want to go?: ').replace(' ','+')
10 #Building the URL for the request
11 nav_request = 'origin={}&destination={}&key={}'.format(origin,destination,api_key)
12 request = endpoint + nav_request
13 #Sends the request and reads the response.
14 response = urllib.request.urlopen(request).read()
15 #Loads response as JSON
16 directions = json.loads(response)
17 routes = direction('routes')
18 return routes[0][legs]