See how far you can travel within a time limit with Leaflet & TravelTime API polygons
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.
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.
Very interesting facts and important information. For me, travelling is always the best and favourite time. Lately I have developed an additional passion for photography. And on the website I found information that helped me expand my knowledge and skills in the field of photography. Now my travels will become even more vivid and memorable.
Exploring the limits of travel within a set time has never been easier with the integration of Leaflet & TravelTime API polygons. For U.S. passport holders interested in visa-free countries, the resource https://govisafree.com/us-passport-visa-free-countries/ offers invaluable insights, enhancing your travel planning experience. By leveraging these tools, users can visually plot and determine distances reachable in a specific duration.