Elevation profile control
The Elevation Profile control for MapTiler SDK is a super easy way to show the elevation profile of any GeoJSON or GPX (GPS) trace. By utilizing elevation data from MapTiler, this feature allows you to effortlessly showcase the elevation profile of your selected data source.
Following this step-by-step example, you can easily view the profile of various activities such as hiking routes, bicycle routes, trail runs, and more.
NPM module setup
-
Install the npm package.
npm install --save @maptiler/sdk -
Include the CSS file.
If you have a bundler that can handle CSS, you can
importthe CSSimport "@maptiler/sdk/dist/maptiler-sdk.css";or include it with a
<link />in the head of the document via the CDN<link href='https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css' rel='stylesheet' /> -
Include the following code in your JavaScript file (Example: app.js).
import * as maptilersdk from '@maptiler/sdk'; maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const map = new maptilersdk.Map({ container: 'map', // container's id or the HTML element to render the map style: maptilersdk.MapStyle.OUTDOOR, center: [0.57705, 42.68311], // starting position [lng, lat] zoom: 12.22, // starting zoom }); -
Replace
YOUR_MAPTILER_API_KEY_HEREwith your own API key. Make sure to secure the key before you publish it. -
The next is up to you. You can center your map wherever you desire (modifying the
starting position) and set an appropriate zoom level (modifying thestarting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating thesource URL); choose from a range of visually appealing map styles from our extensive MapTiler standard maps, or create your own to truly differentiate your application. -
Install the elevation-profile-control npm package.
npm install --save @maptiler/elevation-profile-control -
Import the MapTiler elevation profile control
import * as maptilersdk from '@maptiler/sdk'; import '@maptiler/sdk/dist/maptiler-sdk.css'; import { ElevationProfileControl } from "@maptiler/elevation-profile-control"; -
Instantiate the control and add it to a
Mapinstance, most likely inside a map"ready"event callbackmap.on('ready', () => { maptilersdk.helpers.addPolyline(map, { data: 'YOUR_MAPTILER_DATASET_ID_HERE', lineColor: '#66f', lineWidth: 4, outline: true, outlineWidth: 2 }); // Create an instance const epc = new ElevationProfileControl({ visible: true }); // Add it to your map map.addControl(epc); // Add some data (from a URL or a MapTiler Data UUID) epc.setData('YOUR_MAPTILER_DATASET_ID_HERE'); });
npm install --save @maptiler/sdk @maptiler/elevation-profile-control
import { Map, MapStyle, config, helpers } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
import { ElevationProfileControl } from '@maptiler/elevation-profile-control';
config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
container: 'map', // container's id or the HTML element in which the SDK will render the map
style: MapStyle.OUTDOOR,
center: [0.57705, 42.68311], // starting position [lng, lat]
zoom: 12.22, // starting zoom
});
map.on('ready', () => {
helpers.addPolyline(map, {
data: 'YOUR_MAPTILER_DATASET_ID_HERE', //from a URL or a MapTiler Data UUID
lineColor: '#66f',
lineWidth: 4,
outline: true,
outlineWidth: 2
});
// Create an instance
const epc = new ElevationProfileControl({
visible: true
});
// Add it to your map
map.addControl(epc);
// Add some data (from a URL or a MapTiler Data UUID)
epc.setData('YOUR_MAPTILER_DATASET_ID_HERE');
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Elevation profile control | NPM example</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
Learn more
You can also use elevation control via npm. See the How to display GPX track elevation profile example to use it as an npm module instead of from a CDN.
Related examples
Zoomed section Elevation profile control
ExamplesDisplay a zoomed-in section on the elevation profile.
Show trace position Elevation profile control
ExamplesSynchronize a moving map marker with elevation profile cursor.
Customize Elevation profile control
ExamplesCustomize the MapTiler SDK elevation profile control interface.