Red
Red

Reputation: 2246

Google Maps API: Travel time with current traffic

I am wondering if anyone has a way, using the google APIs (I'm not picky on version) to find out the time of a route with current traffic. I know that the total time of travel with out traffic is available, but I only need that one number.

Upvotes: 1

Views: 2729

Answers (2)

coppaste
coppaste

Reputation: 176

You can use the googlemaps client API to get the driving duration in traffic using google directions API, . For python API, refer github page

Sample code

import googlemaps as gmaps
import datetime
import os

SRC = FROM_ADDRESS
DEST = TO_ADDRESS
CLIENT_KEY = os.getenv('GOOGLE_API_KEY')  # Set your directions api key as an env variable
gmaps_client = gmaps.Client(CLIENT_KEY)

now = datetime.datetime.now()
directions = gmaps_client.directions(SRC, DEST, mode="driving",departure_time=now)

# debug
# print(directions)

directions = directions[0].get('legs')[0].get('duration_in_traffic').get('text')
print(directions)

Upvotes: 1

Dr.Molle
Dr.Molle

Reputation: 117334

Purchase a business-license, and you'll be able to get these informations(when available) by setting the durationInTraffic -option of the directionsRequest to true.

Upvotes: 2

Related Questions