Displaying the elevation profile of a route

A more complex example that, for a route (obtained using the Planning function), retrieves (using the Elevation function) and displays the elevation profile of this route.

The procedure is evident from the code, summarized briefly here:

  • Using the REST API, we plan the route and obtain its geometry.
  • The geometry may contain more than 256 points. Therefore, we select a maximum of 256 points from this geometry to obtain the elevation profile with a single query.
  • Using the REST API, we obtain elevations at these points.
  • We format the result as GeoJSON, which can be processed by the Leaflet Elevation plugin.

Note:
In the example, we assume that the route is relatively short. For longer routes, it is necessary to query elevations gradually (e.g., in segments of 10 km) to obtain sufficient detail of the elevation profile while not exceeding the limitation that all points in the query must be within a 1-degree range.

For demonstration, we will use the Leaflet mapping library. Along with the Leaflet elevation plugin for rendering the connected route and elevation profile.