@watergis/mapbox-gl-legend
v2.0.1
Published
This module adds legend control which is able to create legend panel from mapbox style to mapbox-gl
Readme
mapbox-gl-legend
This module adds legend control which is able to create legend panel from mapbox style to mapbox-gl.
This module is using source code of orangemug/legend-symbol. I just adopted this library to normal Mapbox GL Plugin. Thanks so much to develop this library!
This package was moved from watergis/mapbox-gl-legend to watergis/maplibre-gl-legend monorepo since v2.0.0.
for MapLibre GL JS users
Please consider to use @watergis/maplibre-gl-legend for MapLibre GL JS.
Installation
pnpm i -D @watergis/mapbox-gl-legend mapbox-glDemo
See demo.
See API reference for all classes and options.

Usage
import { MapboxLegendControl } from '@watergis/mapbox-gl-legend';
import '@watergis/mapbox-gl-legend/dist/mapbox-gl-legend.css';
import mapboxgl, { Map } from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
mapboxgl.accessToken = 'your mapbox access token';
const map = new Map({ container: 'map', style: 'your style url' });
// please add legend control after loading stylefiles, otherwise it causes errors...
map.on('load', function () {
const targets = {
pipeline: 'Pipeline',
meter: 'Water Meter',
valve: 'Valve'
};
// add legend control without checkbox, and it will be hide as default
map.addControl(
new MapboxLegendControl(targets, {
showDefault: false,
showCheckbox: false,
onlyRendered: true,
reverseOrder: true
}),
'top-right'
);
// add legend control with all layers
map.addControl(new MapboxLegendControl({}, { reverseOrder: false }), 'bottom-left');
});Specify your layers which you want to add the legend by the control.
Options
All options are optional. See LegendOptions in the API reference for the details.
- showDefault
- true: Legend will be shown as default
- false: Legend will be hide as default
- default value is
trueif you don't specify the option.
- showCheckbox
- true: the checkbox for switching visibility will be added as default
- false: the checkbox will not be added as default
- default value is
trueif you don't specify the option.
- reverseOrder
- true: layers will be ordered from top.
- false: layers will be ordered from bottom.
- default value is
trueif you don't specfify the option.
- onlyRendered
- true: only rendered layers will be shown on legend as default.
- false: all layers' legend will be shown as default.
- default value will be true.
- title
- the text shown above the icons
- default: "Legend"
- onlyRenderedLabel
- the label text of the "Only rendered" checkbox. Use it to translate the label into other languages.
- default: "Only rendered"
- accessToken
- Mapbox access token used to load
mapbox://sprites. - default:
mapboxgl.accessToken
- Mapbox access token used to load
Use CDN
<link href="https://api.mapbox.com/mapbox-gl-js/v3.26.0/mapbox-gl.css" rel="stylesheet" />
<link
href="https://cdn.jsdelivr.net/npm/@watergis/mapbox-gl-legend@latest/dist/mapbox-gl-legend.css"
rel="stylesheet"
/>
<script type="importmap">
{ "imports": { "mapbox-gl": "https://esm.sh/[email protected]" } }
</script>
<script type="module">
import mapboxgl from 'mapbox-gl';
import { MapboxLegendControl } from 'https://cdn.jsdelivr.net/npm/@watergis/mapbox-gl-legend@latest/dist/mapbox-gl-legend.es.js';
mapboxgl.accessToken = 'your mapbox access token';
const map = new mapboxgl.Map({ container: 'map', style: 'your style url' });
map.on('load', () => {
map.addControl(new MapboxLegendControl({}, { showDefault: true }), 'top-right');
});
</script>Development
Copy .env.example to .env and set your Mapbox access token.
pnpm lint
pnpm format
pnpm devopen http://localhost:5173.
Build package
pnpm buildThe modules will be generated under dist folder.
