TRY DEMO APP

Isochrone API tutorial: see how far you can travel within a time limit with Leaflet & TravelTime API polygons

by Niharika Sisodia
on Feb 26, 2019

isochrone-map

We'll show you how to draw travel time polygons, also known as an isochrones with simple HTML and JavaScript code and data provided from TravelTime API on a Leaflet map.

In this example, we will use the TravelTime API Geocoder to get coordinates of the White House, Washington DC, then send those coordinates and trip parameters as a Time Map search back to the API. The received polygon data will be drawn on a Leaflet map. In the end result, you will see which areas on a map can be reached within 60 minutes using public transportation. We will also be able to select from different transportation modes such as walking, cycling, public transport and more.

Code snippets provided in this tutorial are inserted into a CodePen integrated development window where you can manipulate the code and see a live result. To make the sample work and draw the isochrone, you’ll first need to insert an API key into the CodePen - Request an API key for the TravelTime Search API by clicking here.

The basics

To create an isochrone, the user must provide parameters to the API search request, including:

  • Departure or arrival location
  • Mode of transport
  • Travel time limit
  • Time of the day

Once the request is made, the TravelTime API receives the request and returns a JSON response. The response includes coordinates for points. These points show the boundaries of shapes reachable within the travel time.

TravelTime API Time Map request and response

Communication with the API works by sending a JSON format request and the API returns a JSON format response with the polygon data that we will see drawn in the sample code.

Here is how an API request looks without header information, more information about the Time Map search and it's parameters is available in the Travel Time API documentation.

The JSON response after a successful API request includes coordinates. These points show the boundaries of shapes reachable within the corresponding travel time.

Visualizing on a map

This response has to be visualized on a map. Let’s explore the sample code that is also available in the CodePen development environment.

Isochrone_map

The image above shows how this response would be displayed on a Leaflet map. It can be implemented using any programming language or mapping system you prefer. This is the simplest way to generate a request and display a response using JavaScript.

Writing our code sample

First, we need to define parameters for the search.

  • Convert the name of the start location into coordinates using a geocoder;
  • Enter the departure time into the ISO format.
  • Translate the 1 hour travel time into seconds which is 60 minutes x 60 seconds.

These values usually would be in the JSON request, in our sample, we're setting them as variables

As mentioned previously to execute a request to the TravelTime API, an API key is needed, that can be obtained here. The API key is added to the header information of the API request, into variables APPLICATION_ID and API_KEY. These are needed to authenticate the request and usually sit on the server side of your application.

Geocoding

For the starting location, we specified The White House, Washington DC, but the API only accepts latitude and longitude coordinates. To change the address into coordinates we use TravelTime Geocoder.

Request to the API

Now that we have everything we need for the request, we can use the code that would generate the request to API Time Map search. A JSON departure_search is formed and sent in a HTTP request.

Drawing the result

In this sample we are using free Leaflet.js for the interactive map and Mapbox map tiles. mymap variable sets the starting location of the map when the code sample loads. 

Drawing of the isochrone itself is handled by this script.

To see all of the mentioned code in action, please try our CodePen.

See the Pen Drawing isochrones by iGeolise (@igeolise) on CodePen.

Related

IKEA's marketing team uses TravelTime to create a drive time map

IKEA uses TravelTime to build a drive time map. See how their marketing team use it to adjust messaging to their audience.

Evelina Bezubec Oct 9, 2019
READ

5 ways to do geocoding for free

Discover solutions that do geocoding for free. Find out which free geocoders have the best functionality. Convert addresses and postcodes into coordinates.

Evelina Bezubec Sep 9, 2019
READ

5 Google Places API alternatives for points of interest data

Find Google Places API alternatives. Connect to a reliable 'points of interest' database via an API. Improve user experience and perfect your location search tool.

Louisa Bainbridge Jun 14, 2019
READ

The guide to TravelTime API limits

Discover the volume of searches you can do per month and per minute on the TravelTime API free plan. You can use more than one search in an API request.

Louisa Bainbridge May 16, 2019
READ

Step by step free geocoding for Leaflet

Geocoding for Leaflet. Learn how to use a geocoder that is Leaflet-compatible. Convert addresses and postcodes into coordinates to be displayed on a Leaflet map.

Louisa Bainbridge May 7, 2019
READ

TravelTime case study: healthcare development analysis

The Megahospital challenge is a tool created using the TravelTime API by Border City Data. See how the app was built for healthcare development analysis.

Niharika Sisodia Mar 26, 2019
READ