Skip to content
Elevation Control

Elevation Control

@maptoolkit/maplibre-elevation-control shows an elevation profile for a route on a MapLibre GL JS map. Hovering the profile marks the matching position on the route, and dragging across it highlights a section.

Install

npm install @maptoolkit/maplibre-elevation-control maplibre-gl

Usage

import * as maplibregl from "maplibre-gl";
import { ElevationControl } from "@maptoolkit/maplibre-elevation-control";
import "@maptoolkit/maplibre-elevation-control/style.css";

const map = new maplibregl.Map({
  container: "map",
  style: "https://styles.maptoolkit.org/hiking.json",
  center: [11.389, 47.29],
  zoom: 12,
});

const response = await fetch("route.geojson");
map.addControl(new ElevationControl(await response.json()));

The route is a GeoJSON Feature with a LineString or MultiLineString geometry. Every coordinate needs its height in meters as a third value: [lng, lat, ele]. The control does not look up heights, so the route has to bring them, for example a track recorded with GPS. The community services have no elevation lookup; the Elevation API on docs.maptoolkit.com returns the height for each coordinate and needs an API key.

The control draws the route on the map itself and sits in the bottom-right corner. Pass a position to addControl to move it.

Without a bundler

The package is an ES module only. Load it from a CDN and map maplibre-gl in an import map:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-elevation-control@1.0.0/dist/maplibre-elevation-control.css" />

<script type="importmap">
  {
    "imports": {
      "maplibre-gl": "https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.mjs"
    }
  }
</script>
<script type="module">
  import * as maplibregl from "maplibre-gl";
  import { ElevationControl } from "https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-elevation-control@1.0.0/dist/maplibre-elevation-control.js";

  const map = new maplibregl.Map({
    container: "map",
    style: "https://styles.maptoolkit.org/hiking.json",
    center: [11.389, 47.29],
    zoom: 12,
  });

  const response = await fetch("route.geojson");
  map.addControl(new ElevationControl(await response.json()));
</script>

Options, events and styling

Section bars (for example surface or road type), the map layers the control draws, the translations, the events, the methods and the CSS custom properties are described in the Elevation Control reference on docs.maptoolkit.com.

License

The Elevation Control is open source under the BSD 3-Clause License.