Wednesday, February 15, 2017

Google Traffic API using Python or PowerShell

UPDATE: 2/16/2017 - small tweak needed to the scripts. I changed the JSON element to duration_in_traffic. This will show the duration plus traffic. Duration will just show how long it should take with no traffic.

I have visited this area a few times. I needed a clean solution to get drive times for my commute home. Fortunately Google Maps Distance Matrix has an easy to use API. Of course you will need to enable the API in your Google Dev Console and get your API key to use the traffic feature.

Once your have your key its a cinch. 
  1. Grab your origination and destination LAT, LONG coordinates. Just go to google maps, find your location, right click on the map and select What's Here from the menu. You will see your coordinates.

  2. Plug these into the script 
  3. You can tweak the model if you want. Distance Matrix has 3 traffic models, best-guess, pesimistic, and optimistic.
  4. Run the script

Powershell


Python

In order to get the Python Script to work we have to bypass the SSL verification. Probably not the best idea for production type implementations but it is a workaround.


You can find more information about the Distance Matrix API here: https://developers.google.com/maps/documentation/distance-matrix/

0 comments:

Post a Comment