Calculate distance in Google spreadsheet
The challenge: calculate the distance between two locations (zip codes, addresses, etc.) using a spreadsheet. The distance should be based on driving directions.
The solution: Google Maps has a nice API and Google spreadsheets has a function (importxml) that we can use for this. Quite useful for quickly finding distances to use in your mileage reports.
Not interested in all of the nitty gritty details? Simply download the spreadsheet template.
Update (July 2011): now with button that adds the results to a separate log sheet.
Update (February 2012): now with map images of From and To locations, version information, latitude/longitude information.
A ready-to-go spreadsheet with all of the examples is available as a template. Google account required.
Fill out From and To locations and the spreadsheet will calculate the distance and driving time based on the recommended route. You can copy the result to a trip log.
How to get distance data from Google Maps in your spreadsheet?
- create a URL that uses the Google Maps Directions API which returns XML output;
- use the Google spreadsheets function importxml() to parse the XML file, and use an XPath command to extract the info you need.
Google Maps API for directions
Nice: no API key is needed. See results (unformatted XML output).
- the Google Maps API for directions is well documented
- in your spreadsheet point the origin=… and destination=… to spreadsheet cells that contain the origin and destination, the importxml() commands will be run every time you change the URL. Nice.
- sensor=false: don’t use browser location sensor (required).
Some of the optional parameters:
- units=metric: show km (alternative: imperial)
- region=nl: country that should be used as default for origin or destination
- language=nl: language to use for the results
- alternatives=false: do not show alternative routes, only show the recommended one
The resulting XML file contains 1 route (or if you ask for alternatives: 1 or more), and for each route 1 leg (I am not using via waypoints), and each leg consists of steps (turn left at …, etc.).
Example parse commands
Assumed: the URL is created in cell B20. Please note: you can have a maximum of 50 importxml commands in a spreadsheet.
The base command to use to get e.g. the distance (depending on your units used: in feet or meters):
The first argument is the URL, the second argument is the XPath command.
Show the start and finish (e.g. when using “brentwood” as origin, it will show: “Brentwood, TN, USA”)
The XPath predicate  and [last()] are used in case the route has multiple legs (in our examples they don’t)
Alternatively you could use the spreadsheet index() function:
Show the label of the preferred route (e.g. “A28 and A73″):
Calculate the distance of the preferred route:
The result of this field is always in meters, so we divide the result by 1000 to get kilometers, and we round it up.
And calculate the duration (the result is in seconds, so divide the result by 60 to get the minutes):
You can also ask for alternatives, assuming &alternatives=true is added to the URL (stored in B21), the same function that showed 1 route summary, now shows a list.
The spreadsheet automatically sees it is an array of results that need to be displayed.
Let’s calculate the average distance of the routes:
Average works fine with an array.
The spreadsheet will get confused if you try to perform a calculation on an array, but that is easy to fix using arrayformula().
The XML contains latitude + longitude information, each step of the route has a lat+long pair.
The lat/long of the start location can be found in the the first step of the route list.
=importxml(B20,"//leg/step/start_location/lat") &" "& importxml(B20,"//leg/step/start_location/lng")
The lat/long of the destination can be found in the last step of the route list, there’s an XPath predicate for that: [last()].
=importxml(B20,"//leg/step[last()]/end_location/lat") &" "& importxml(B20,"//leg/step[last()]/end_location/lng")