Saturday 9 June 2018

HouseScanner: Using Google Maps and JSON to get Travel Time

This post describes how to extract JSON data from Google Maps. I need that for estimating the travel time from a house to my work.

Google Maps offers an API for querying directions. For example, a bicycle ride from Staffanstorp to Lomma, both in Skåne, Sweden is queried like this:
http://maps.googleapis.com/maps/api/directions/json?origin=Storgatan+4%2C+Staffanstorp%2C+Sweden&destination=Centrumgatan+13%2C+Lomma%2C+Sweden&mode=bicycling

The resulting file contains some useful information:
The total travel time is associated with the "duration" tag. The directions are listed under the "steps" tag.

A Google Maps set of bicycle directions between Staffanstorp and Lomma.
In order to retreive the duration, a query is sent with the origin and end addresses on the form; "Storgatan 4, Staffanstorp, Sweden".

The duration is found by browsing through the JSON tree to the tag that I need.

In my particular case, I compare the bicycle time with the public transit time at three different occasions. The shortest time is returned.

No comments:

Post a Comment