It is possible to get traffic data.
Below is my implementation in python. The API has some quota & is not fully free, but good enough for small projects
import requests
import time
import json
while True:
url = "https://maps.googleapis.com/maps/api/distancematrix/json"
querystring = {"units":"metric","departure_time":str(int(time.time())),"traffic_model":"best_guess","origins":"ITPL,Bangalore","destinations":"Tin Factory,Bangalore","key":"GetYourKeyHere"}
headers = {
'cache-control': "no-cache",
'postman-token': "something"
}
response = requests.request("GET", url, headers=headers, params=querystring)
d = json.loads(response.text)
print("On", time.strftime("%I:%M:%S"),"time duration is",d['rows'][0]['elements'][0]['duration']['text'], " & traffic time is ",d['rows'][0]['elements'][0]['duration_in_traffic']['text'])
time.sleep(1800)
print(response.text)
Response is :-
{
"destination_addresses": [
"Tin Factory, Swamy Vivekananda Rd, Krishna Reddy Industrial Estate, Dooravani Nagar, Bengaluru, Karnataka 560016, India"
],
"origin_addresses": [
"Whitefield Main Rd, Pattandur Agrahara, Whitefield, Bengaluru, Karnataka 560066, India"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "10.5 km",
"value": 10505
},
"duration": {
"text": "35 mins",
"value": 2120
},
"duration_in_traffic": {
"text": "45 mins",
"value": 2713
},
"status": "OK"
}
]
}
],
"status": "OK"
}
You need to pass "departure_time":str(int(time.time()))
is a required query string parameter for traffic information.
Your traffic information would be present in duration_in_traffic
.
Refer this documentation for more info.
https://developers.google.com/maps/documentation/distance-matrix/intro#traffic-model