How to display a map legend control to toggle layers visualization
This tutorial demonstrates the process of presenting a map legend control that allows for the toggling of layer visualization.
By following this guide, users will learn how to display a control panel that showcases the various layers of a map and enables them to easily turn on or off specific layers as needed. This functionality provides a convenient way for users to customize their map viewing experience, allowing them to focus on specific layers of interest while hiding others.
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.STREETS, center: [139.75626, 35.68254], // starting position [lng, lat] zoom: 13.85, // 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. -
Include the
maplibre-gl-legendJavaScript and CSS files in theheadof your HTML file. To create the legend we will use the maplibre-gl-legend plugin.<link href='https://www.unpkg.com/@watergis/maplibre-gl-legend@latest/dist/maplibre-gl-legend.css' rel='stylesheet' /> <script src="https://www.unpkg.com/@watergis/maplibre-gl-legend@latest/dist/maplibre-gl-legend.umd.js"></script> -
Add event handler for map
loadevent. You will add code to create a vector source and a vector layer in this handler.map.on('load', function() { }); -
Create the list of layers that we want to show in the legend. For each layer we must create a key:value pair where the key is the ID of the layer in the map style and the value is text that is displayed in the legend.
map.on('load', function() { const targets = { Residential: "Residential", Water: "Water", Building: "Building", Airport: "Airport", "Other POI": "Pois", }; }); -
Add the
maplibre-gl-legendcontrol to the map.map.on('load', function() { const targets = { Residential: "Residential", Water: "Water", Building: "Building", Airport: "Airport", "Other POI": "Pois", }; const options = { showDefault: true, showCheckbox: true, onlyRendered: true, reverseOrder: true }; map.addControl(new MaplibreLegendControl.MaplibreLegendControl(targets, options), "bottom-left"); });
Learn more
Check out the MapTiler Planet schema to know how it is organized into different thematic layers and which attribute and values each layer contains.
Visit the JavaScript Maps API reference to learn how to create your custom control.
Related examples
Change attribution default position
ExamplesPosition map attribution in the top-left corner on initialization.