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.
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.
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.
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.