Calculating distance using a Google Maps​

Calculating distance using Google Maps

This article demonstrates how to get the distance between two locations using a Google API in the knowledge map, allowing the user to enter a postcodes for Rainbird to calculate their distance or travel duration. 

To demonstrate how Rainbird can use data from Google Maps to complete this query, this article will explain how to build a “Calculate distance” map that uses data injected from a Google map. The API we are using for this map allows us to calculate the distance in kilometres or the travel time in seconds. Both values can be converted to suit the purpose (i.e. miles, minutes)


This Mechanic becomes most useful to check the distance between two locations. For example to calculate how much time it would take the user to travel to an underground station or how many miles are between the user’s address and a hotel address. The results can be used for further calculations and might provide the closest underground station or how much petrol/battery time is required to travel to the hotel location.


When can this pattern be applied?

  • When calculating the distance between two locations 

  • When estimating travel duration between two locations 


The map only calculates the distance between 2 UK post codes, so foreign postcodes won’t be recognised. 

To create this map we used the Generic structures of the Elbow, Triangle technique and API call

Figure 1: Generic build Structures with API connector


Both “Post code” and “Train station” are string concepts.

This example queries the distance between the user’s postcode and the train station postcode. 

The relationship “has travel distance” returns the travel duration in seconds, allowing the user to check the duration between two post codes.

Follow the steps to recreate the build pattern example:

Create four string concepts:

  • Person’ – the Subject 
  • Post Code’ – the Subjects postcode
  • Train Station’ – the train station postcode
  • Distance’ – the travel duration between the user and the train station 

Create the following relationships:

  • has postcode – asks the user to enter his postcode (we created the instance SE1 3RU: Kipling St.)
  • goes to train station (we created the instance SE1 9SP: London Bridge)
  • to the train station

Figure 2: Map framework

Now it’s time to create the Google API that will calculate the distance in the knowledge map.

Click on a white space in Rainbird Studio and select ‘Add Datasource’. 

Title the datasource as ‘Travel time’; naming the datasource helps to distinguish multiple datasources in your map.

Enter ‘Person’ within the Subject (%S) field underneath: ‘Person’ is our subject for the facts the datasource will create.

Next, open the ‘Input variables’ section to create the following variables: 

%S [Person] has post code %POST_CODE 

%POST_CODE to the train station %TRAIN_STATION

Figure 3: Create the subject of the datasource and both variables

Both {{variables}} are used in the following API Request field.

Figure 4: GET API Request

The URL can be copy/pasted into your map:{{%POST_CODE}}&destination={{%TRAIN_STATION}}&transit&key=AIzaSyBDIPUqSDI9Fcs6dsijb2CY8DHYlN8w3mU

The API call is contructed in 3 parts:

This part of the API call tells Rainbird to open the URL – access it using an api – get directions, return in json code with both destinations: 


This part of the API call provides the URL with our  start and end variables (origin and destination)


The last part of the API calls tells the URL that the user is asking for the distance by public transport (transit) and providing the API key.

We skip the Advanced section of the panel and open the ‘Output Facts’ section to create the following outcome for our main relationship ‘has travel distance:

Figure 5: has travel distance Outcome

This section in this API URL provides the path segments where to find the information. As shown in Figure 6, to return the time in seconds return the value listed in duration -> value.

Note: To return the distance we would choose distance -> value

Figure 6: display of the full API

The values can be easily converted into different measures, using mathematical functions:

Convert: distance result / 1609.344 = km in miles
duration result / 3600 = seconds in hours

Click on the ‘Export.rbird’ button to download the ‘Calculate Distance’ map used in this example. The knowledge map can then be imported into your Rainbird Studio.

Query and Result

The query is build as usual on the rule ‘has travel distance’. The outcome of the query will be distance in seconds.

Article Feedback form
Did you find this article useful?