@watergis/maplibre-gl-legend
Version:
This module adds legend control which is able to create legend panel from mapbox style to mapbox-gl-legend
156 lines (127 loc) • 5.14 kB
Markdown
# maplibre-gl-legend


This module adds legend control which is able to create legend panel from mapbox style to maplibre-gl. It was forked from [mapbox-gl-legend](https://github.com/watergis/mapbox-gl-legend).
This module is using source code of [orangemug/legend-symbol](https://github.com/orangemug/legend-symbol). I just adopted this library to normal Mapbox GL Plugin. Thanks so much to develop this library!
## Installation:
```bash
pnpm i -D @watergis/maplibre-gl-legend
```
## Demo:
Try [codesandbox](https://codesandbox.io/s/mapbox-gl-legend-0x6f0).
See [demo](https://maplibre-gl-legend.water-gis.com).

## Usage:
```ts
import { MaplibreLegendControl } from "@watergis/maplibre-gl-legend";
import '@watergis/maplibre-gl-legend/dist/maplibre-gl-legend.css';
import maplibregl from 'maplibre-gl';
const map = new maplibregl.Map();
//please add legend control after loading mapbox stylefiles, otherwise it causes errors...
map.on('load', function() {
const targets = {
'pipeline': 'Pipeline',
'pipeline_annotation': 'Pipeline Label',
'meter': 'Water Meter',
'flow meter': 'Flow Meter',
'valve': 'Valve',
'firehydrant': 'Fire Hydrant',
'washout': 'Washout',
'tank': 'Tank',
'tank_annotation': 'Tank Label',
'wtp': 'WTP',
'wtp_annotation': 'WTP Label',
'intake': 'Intake',
'intake_annotation': 'Intake Label',
'parcels': 'Parcels',
'parcels_annotation': 'Parcels Label',
'village': 'Village',
'village_annotation': 'Village Label',
'dma': 'DMA',
'dma_annotation': 'DMA Label',
};
// add legend control without checkbox, and it will be hide as default
map.addControl(new MaplibreLegendControl(targets, {showDefault: false, showCheckbox: false, onlyRendered: false }), 'top-right');
// add legend control with checkbox, and it will be shown as default
map.addControl(new MaplibreLegendControl(targets, {showDefault: true}), 'bottom-right');
// add legend control with all layers, and it reverse layer order
map.addControl(new MaplibreLegendControl({}, {reverseOrder: false}), 'bottom-left');
// if you want to use basemap from Mapbox in v2
map.addControl(new MaplibreLegendControl({}, {accesstoken: 'your mapbox accesstoken'}));
```
Specify your layers which you want to add the legend by the control.
### Options
- showDefault
- true: Legend will be shown as default
- false: Legend will be hide as default
- default value is `true` if 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 `true` if you don't specify the option.
- reverseOrder
- true: layers will be ordered from top.
- false: layers will be ordered from bottom.
- default value is `true` if 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"
### Use CDN
```html
<link href='https://www.unpkg.com/@watergis/maplibre-gl-legend@latest/dist/maplibre-gl-legend.css' rel='stylesheet' />
<script src='https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.js'></script>
<script src="https://www.unpkg.com/@watergis/maplibre-gl-legend@latest/dist/maplibre-gl-legend.umd.js"></script>
<script>
map.on('load', function() {
const targets = {
'pipeline': 'Pipeline',
'pipeline_annotation': 'Pipeline Label',
'meter': 'Water Meter',
'flow meter': 'Flow Meter',
'valve': 'Valve',
'firehydrant': 'Fire Hydrant',
'washout': 'Washout',
'tank': 'Tank',
'tank_annotation': 'Tank Label',
'wtp': 'WTP',
'wtp_annotation': 'WTP Label',
'intake': 'Intake',
'intake_annotation': 'Intake Label',
'parcels': 'Parcels',
'parcels_annotation': 'Parcels Label',
'village': 'Village',
'village_annotation': 'Village Label',
'dma': 'DMA',
'dma-annotation': 'DMA Label',
'contour-line': 'Countour',
'contour-label': 'Contour Label',
'hillshade': 'Hillshade'
};
map.addControl(new MaplibreLegendControl.MaplibreLegendControl(targets, {
showDefault: false,
showCheckbox: false,
onlyRendered: true,
reverseOrder: true
}), 'top-right');
})
</script>
```
Furthermore, you may download specific version's scripts and css locally from [NPM](https://www.npmjs.com/package/@watergis/maplibre-gl-legend) page.
See an example HTML at [index_umd.html](./index_umd.html)
## Development
```
pnpm lint
pnpm format
pnpm dev
```
open [http://localhost:5173](http://localhost:5173).
If there are any changes on source code, it will be reflected automatically.
## Build package
```
pnpm build
```
The modules will be generated under `dist` folder.