zakjan / leaflet-lasso

Lasso selection plugin for Leaflet
https://zakjan.github.io/leaflet-lasso/
MIT License
111 stars 21 forks source link

leaflet-lasso

Lasso selection plugin for Leaflet

Demo

Screenshot

Supports all Leaflet vector layers:

Selection modes:

Install

npm install leaflet-lasso

or

<script src="https://unpkg.com/leaflet-lasso@2.2.13/dist/leaflet-lasso.umd.min.js"></script>

Usage

import "leaflet-lasso";

For detailed API, please see exported TypeScript typings.

Handler

Use for custom activation.

interface LassoHandlerOptions {
    polygon?: L.PolylineOptions,
    intersect?: boolean;
}
const lasso = L.lasso(map, options);
yourCustomButton.addEventListener('click', () => {
    lasso.enable();
});

Control

Use for default control.

interface LassoControlOptionsData  {
    title?: string;
}

type LassoControlOptions = L.ControlOptions & LassoControlOptionsData & LassoHandlerOptions;
L.control.lasso(options).addTo(map);

Finished event

Listen for this event to receive matching Leaflet layers.

interface LassoHandlerFinishedEventData {
    originalEvent: MouseEvent;
    latLngs: L.LatLng[];
    layers: L.Layer[];
}

type LassoHandlerFinishedEvent = L.LeafletEvent & LassoHandlerFinishedEventData;
map.on('lasso.finished', (event: LassoHandlerFinishedEvent) => {
    console.log(event.layers);
});

Thanks

Icon by @Falke-Design