Using the TomTom Live Traffic API
Update June 2016: TomTom has published official documentation: TomTom Developer Portal
As of late, I’ve been commuting to work on a very congested part of the Dutch highway network. I’ve been using the free online TomTom Live Traffic to see if there are jams on the motorways, major roads or secondary roads. To date, it is by far the best traffic measurement tool I stumbled upon. It updates every two minutes, also works inside cities and has the most accurate information.
I would have loved to use the traffic information for other purposes, like a nice widget or to gather statistical information, but the API appears to be accessible to large corporations only. So it’s time to pull out Firebug!
So what have I figured out so far?
The request URI for traffic information is similar to the sample below:
Let’s dig a little deeper:
Using this request the following data is returned:
So what does it all mean? After playing around with various samples, I figured out it uses the following JSON scheme:
The JSON format is actually very easy. You have the “poi” node which contains an array of all the traffic jams. These traffic jam items contain various information bits about the traffic jam, like the length, duration, cause, location, start and end.
But sometimes, several jams are clustered into one icon. This happens when a lot of traffic jams have formed in one area. When this happens, the clustered traffic jams are put in the “cpoi” array and the amount of traffic jams is put in the “cs” field. We also get a “cbl” and a “ctr” field with coordinates, presumably the cluster’s bottom-right and top-left coordinates. The “f”, “t”, “r”, “dl” and “c” fields are all hidden when the poi is a clustered traffic jam, these are only available on a non-clustered traffic jam.
Some pointers: The “cpoi” array is limited to 30 traffic jams, but the “cs” field will always display the actual amount. The expandCluster parameter in the request URI ensures that the cpoi array is filled with data. When the expandCluster parameter is false, the cpoi array will be an empty array.
Building an application
Based on the URI and JSON scheme, I built a small Ext JS based web application to gather the traffic information based on a form containing the user input. Within the application I used the Proj4js library to convert the EPSG:900913 coordinates to WGS84 coordinates.
The web application can be downloaded from the associated Github repository. Feel free to mess about with the application, like gathering statistics or creating widgets.
If you have any questions, don’t hesitate to use the comments section.
Disclaimer: This is not an official API, I am not a TomTom representative and I am not responsible for any mess you create. Use the API responsibly.