Calculate distances in “Google Sheets” (via Maps API)

Sven Breckler
1 min readApr 5, 2018

--

I love Google Docs with Google Sheets, Docs etc. It’s super easy to work with and to share documents for realtime collaboration.

Last time I created a Sheet which should be a comparative of hotels for my next trip with a friend, so that both of us could add hotels, rate them, see the distances between the hotels and the beach to take the final decision.

For this reason, I wanted to automatically calculate the distance between our list of hotels and the beach. Do to so I used the IMPORTXML() function of Sheets together with the MAPS API.

Here what it looks like:

=IMPORTXML(“https://maps.googleapis.com/maps/api/directions/xml?origin=lloret de mar beach&destination=”&B5&”&key=YOUR_API_KEY&region=es&mode=walking”;”//leg/distance/value”)/1000)

origin: the beach of Lloret de Mar for example
destination: the cell B5 with the name of the hotel (fincalloret.es)
key: your api key (get one here: https://console.developers.google.com/apis)
region: defines de country (! to find the right places) in my case Spain (es)
mode: walking/driving… in my case walking

Official Documentation of Google Maps API:
https://developers.google.com/maps/documentation/directions/

Greetings from Luxembourg,
Sven

--

--