Skip to content

Instantly share code, notes, and snippets.

@Kevin-De-Koninck
Created May 14, 2017 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kevin-De-Koninck/bafceafe1ed16784962a689b3a90f0c4 to your computer and use it in GitHub Desktop.
Save Kevin-De-Koninck/bafceafe1ed16784962a689b3a90f0c4 to your computer and use it in GitHub Desktop.
Get estimated travel times using Python and Google's distance matrix
#!/usr/bin/python
# This script wil get the estimated travel times between your home and your work (in both directions).
#
# You could use this script to map the estimated travel times of a day in the future
# To plot grpahs you could use: http://plot.ly
import simplejson
import urllib
from datetime import date
from datetime import datetime
import time
import threading
# Set the day of your simulation (must be day in future)
d = date(2017, 5, 4) #yyyy,mm,dd
# To get cooordinates -> Google maps -> right-click -> What's here -> coordinates see bottom of card
coord1 = "51.238525,4.816091" # Home
coord2 = "50.824989,3.304699" # Work
# API keys (Google)
# request them here: https://developers.google.com/maps/documentation/distance-matrix/get-api-key
# I use 2 API's to bypass the limitations
API1 = "AIzaSyBALmzaF_SRIpY27Tf8-3qmkS11O1q0yYs" # Non existing example key
API2 = "AIzaSyD8pZR17Qno65XqYI-6XlZAfR222RUldvM" # Non existing example key
# Creates the needed UNIX timestamp
unixtime = int(time.mktime(d.timetuple())) #+ 2*60*60 # UTC+2 = local time
def getResponseFromGoogleAndLogResult(inputTime):
# Create the GET requests
url_12 = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + coord1 + "&destinations=" + coord2 + "&mode=driving&traffic_model=best_guess&departure_time=" + str(inputTime) + "&language=en-EN&sensor=false&key=" + API1
url_21 = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + coord2 + "&destinations=" + coord1 + "&mode=driving&traffic_model=best_guess&departure_time=" + str(inputTime) + "&language=en-EN&sensor=false&key=" + API2
# Preform the request and read in the JSON data
result_12 = simplejson.load(urllib.urlopen(url_12))
result_21 = simplejson.load(urllib.urlopen(url_21))
# Get the driving time in seconds
driving_time_seconds_12 = result_12['rows'][0]['elements'][0]['duration_in_traffic']['value']
driving_time_seconds_21 = result_21['rows'][0]['elements'][0]['duration_in_traffic']['value']
# Append results to the file
target = open("Results.csv", 'a')
target.write(datetime.fromtimestamp(inputTime).strftime('%Y-%m-%d %H:%M') + ";" + str(driving_time_seconds_12) + ";" + str(driving_time_seconds_21) + "\n")
target.close()
for n in range(0, 1440):
if n%60 == 0:
print("Current progress: " + str(int(n/60)) + "/24h")
getResponseFromGoogleAndLogResult(n*60+unixtime)
@mp504
Copy link

mp504 commented May 7, 2024

Can you get for like the past month. I mean it gives you the time taken for the past month

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment