Gathering detailed insights and metrics for leaflet.pm
Gathering detailed insights and metrics for leaflet.pm
Gathering detailed insights and metrics for leaflet.pm
Gathering detailed insights and metrics for leaflet.pm
@types/leaflet.pm
TypeScript definitions for leaflet.pm
leaflet.pm-snapignore
A modified leaflet.pm allowing ignoring layers during snapping
leaflet.pm-measure-path
This package combine leaflet.pm package with leaflet-measure-path package to enhance the drawing tool with measurements
leaflet.pm-forked
A Leaflet Plugin For Editing Geometry Layers in Leaflet 1.0
🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
npm install leaflet.pm
Typescript
Module System
Node Version
NPM Version
72.4
Supply Chain
99
Quality
75.8
Maintenance
100
Vulnerability
98.9
License
2.18.0
Published on 13 Dec 2024
2.17.0
Published on 15 May 2024
2.16.0
Published on 19 Feb 2024
Custom Rotation, More Events & Fixes
Published on 22 Nov 2023
Various Fixes and new Pro Pricing
Published on 19 Feb 2023
Fix install with latest node versions
Published on 18 Jan 2023
JavaScript (98.54%)
CSS (1.19%)
HTML (0.26%)
Shell (0.01%)
Total Downloads
853,119
Last Day
242
Last Week
1,493
Last Month
8,540
Last Year
117,227
2,243 Stars
1,201 Commits
431 Forks
39 Watching
5 Branches
84 Contributors
Latest Version
2.2.0
Package Id
leaflet.pm@2.2.0
Unpacked Size
166.24 kB
Size
43.76 kB
File Count
5
NPM Version
6.10.2
Node Version
12.9.1
Cumulative downloads
Total Downloads
Last day
-34.9%
242
Compared to previous day
Last week
-31.4%
1,493
Compared to previous week
Last month
-9%
8,540
Compared to previous month
Last year
-22.7%
117,227
Compared to previous year
4
1
Leaflet Plugin For Creating And Editing Geometry Layers
Draw, Edit, Drag, Cut and Snap Layers
Supports Markers, CircleMarkers, Polylines, Polygons, Circles, Rectangles, LayerGroups, GeoJSON and MultiPolygons
Are you using leaflet.pm for commercial projects?
Check out Geoman and consider subscribing to the Leaflet.PM Supporter Plan to support development of advanced leaflet.pm features. You will also get prioritized support and consultation.
npm i leaflet.pm
Download
leaflet.pm.css
and
leaflet.pm.min.js
and include them in your project.
CSS
1 2<link rel="stylesheet" href="https://unpkg.com/leaflet.pm@latest/dist/leaflet.pm.css" />
JS
1<script src="https://unpkg.com/leaflet.pm@latest/dist/leaflet.pm.min.js"></script>
1import 'leaflet.pm'; 2import 'leaflet.pm/dist/leaflet.pm.css';
Just include leaflet.pm.min.js
right after Leaflet. It initializes itself. If
you want certain layers to be ignored by leaflet.pm, pass pmIgnore: true
to
their options when creating them. Example:
1L.marker([51.50915, -0.096112], { pmIgnore: true }).addTo(map);
You can add a toolbar to the map to use leaflet.pm features via a user interface.
1// add leaflet.pm controls with some options to the map 2map.pm.addControls({ 3 position: 'topleft', 4 drawCircle: false, 5});
See the available options in the table below.
Option | Default | Description |
---|---|---|
position | 'topleft' | toolbar position, possible values are 'topleft' , 'topright' , 'bottomleft' , 'bottomright' |
drawMarker | true | adds button to draw markers |
drawCircleMarker | true | adds button to draw circle markers |
drawPolyline | true | adds button to draw rectangle |
drawRectangle | true | adds button to draw rectangle |
drawPolygon | true | adds button to draw polygon |
drawCircle | true | adds button to draw circle |
editMode | true | adds button to toggle edit mode for all layers |
dragMode | true | adds button to toggle drag mode for all layers |
cutPolygon | true | adds button to cut a hole in a polygon |
removalMode | true | adds a button to remove layers |
If you are wondering how e.g. the drawPolygon
button will enable drawing mode
with specific options, here it is: Simply enable drawing mode programatically,
pass it your options and disable it again. The options will persist, even when
the mode is enabled/disabled via the toolbar.
Example:
1// make markers not snappable during marker draw 2map.pm.enableDraw('Marker', { snappable: false }); 3map.pm.disableDraw('Marker'); 4 5// let polygons finish their shape on double click 6map.pm.enableDraw('Polygon', { finishOn: 'dblclick' }); 7map.pm.disableDraw('Polygon');
All available options are specified in the Drawing Mode Section below.
Use Drawing Mode on a map like this
1// enable polygon drawing mode 2map.pm.enableDraw('Polygon', { 3 snappable: true, 4 snapDistance: 20, 5}); 6 7// disable drawing mode 8map.pm.disableDraw('Polygon');
Currently available shapes are Marker
, Circle
, Line
, Rectangle
, Polygon
and Cut
.
You can get an array of all available shapes with:
1map.pm.Draw.getShapes();
See the available options in the table below.
Option | Default | Description |
---|---|---|
snappable | true | enable snapping to other layers vertices for precision drawing. Can be disabled by holding the ALT key. |
snapDistance | 20 | the distance to another vertex when a snap should happen |
snapMiddle | false | allow snapping to the middle of a layers segments (between two vertexes) |
tooltips | true | show helpful tooltips for your user |
allowSelfIntersection | true | allow self intersections |
templineStyle | { color: 'red' }, | leaflet path options for the lines between drawn vertices/markers. |
hintlineStyle | { color: 'red', dashArray: [5, 5] } | leaflet path options for the helper line between last drawn vertex and the cursor. |
cursorMarker | true | show a marker at the cursor |
finishOn | null | leaflet layer event to finish the drawn shape, like 'dblclick' . Here's a list. |
markerStyle | { draggable: true } | leaflet marker options (only for drawing markers). |
You can listen to map events to hook into the drawing procedure like this:
1map.on('pm:drawstart', e => { 2 console.log(e); 3});
Here's a list of map events you can listen to:
Event | Params | Description |
---|---|---|
pm:drawstart | e | Called when drawing mode is enabled. Payload includes the shape type and working layer |
pm:drawend | e | Called when drawing mode is disabled. Payload includes the shape type. |
pm:create | e | Called when a shape is drawn/finished. Payload includes shape type and the drawn layer |
There are also several events for layers during draw. Register an event like this:
1// listen to vertexes being added to currently drawn layer (called workingLayer) 2map.on('pm:drawstart', ({ workingLayer }) => { 3 workingLayer.on('pm:vertexadded', e => { 4 console.log(e); 5 }); 6});
Here's a list of layer events you can listen to:
Event | Params | Description |
---|---|---|
pm:vertexadded | e | Called when a new vertex is added. Payload includes the new vertex, it's marker, index, working layer and shape type |
pm:snapdrag | e | Fired during a marker move/drag. Payload includes info about involved layers and snapping calculation. |
pm:snap | e | Fired when a vertex is snapped. Payload is the same as in snapdrag |
pm:unsnap | e | Fired when a vertex is unsnapped. Payload is the same as in snapdrag |
pm:centerplaced | e | Called when the center of a circle is placed/moved. |
For making the snapping to other layers selective, you can add the "snapIgnore" option to your layers to disable the snapping to them during drawing.
1L.geoJSON(data,{
2 snapIgnore : true
3})
4//This layer will be ignored by the snapping engine during drawing
Let's you edit vertices of layers. Use it like this:
1// enable edit mode 2layer.pm.enable({ 3 allowSelfIntersection: false, 4});
See the available options in the table below.
Option | Default | Description |
---|---|---|
snappable | true | Enable snapping to other layers vertices for precision drawing. Can be disabled by holding the ALT key. |
snapDistance | 20 | The distance to another vertex when a snap should happen. |
allowSelfIntersection | true | Allow/Disallow self-intersections on polygons and polylines. |
preventMarkerRemoval | false | Disable the removal of markers/vertexes via right click. |
The following methods are available for layers under layer.pm
:
Method | Returns | Description |
---|---|---|
enable(options ) | - | Enables edit mode. The passed options are preserved, even when the mode is enabled via the Toolbar. |
disable() | - | Disables edit mode. |
toggleEdit(options ) | - | Toggles edit mode. Passed options are preserved. |
enabled() | Boolean | Returns true if edit mode is enabled. false when disabled. |
hasSelfIntersection() | Boolean | Returns true is the layer has a self intersection. |
You can listen to events related to editing on events like this:
1// listen to when a layer is changed in edit mode 2layer.on('pm:edit', e => { 3 console.log(e); 4});
The following events are available on a layer instance:
Event | Params | Description |
---|---|---|
pm:edit | e | Fired when a layer is edited. |
pm:vertexadded | e | Fired when a vertex is added |
pm:vertexremoved | e | Fired when a vertex is removed |
pm:markerdragstart | e | Fired when dragging of a marker which corresponds to a vertex starts |
pm:markerdragend | e | Fired when dragging of a vertex-marker ends |
pm:snap | e | Fired when a vertex-marker is snapped to another vertex. Also fired on the marker itself. |
pm:unsnap | e | Fired when a vertex-marker is unsnapped from a vertex. Also fired on the marker itself. |
pm:intersect | e | When allowSelfIntersection: false , this event is fired as soon as a self-intersection is detected. |
pm:centerplaced | e | Fired when the center of a circle is moved |
You can enable Edit Mode for all layers on a map like this:
1// enable global edit mode 2map.pm.toggleGlobalEditMode(options);
The following methods are available on map.pm
:
Method | Returns | Description |
---|---|---|
enableGlobalEditMode(options ) | - | Enables global edit mode. |
disableGlobalEditMode() | - | Disables global edit mode. |
toggleGlobalEditMode(options ) | - | Toggles global edit mode. |
globalEditEnabled() | Boolean | Returns true if global edit mode is enabled. false when disabled. |
You can also listen to specific edit mode events on the map instance like this:
1map.on('pm:globaleditmodetoggled', e => { 2 console.log(e); 3});
The event has an object with an enabled boolean and a reference to the map.
1// toggle drag mode like this: 2map.pm.toggleGlobalDragMode();
The following methods are available on map.pm
:
Method | Returns | Description |
---|---|---|
toggleGlobalDragMode() | - | Toggles global drag mode. |
globalDragModeEnabled() | Boolean | Returns true if global drag mode is enabled. false when disabled. |
The following events are available on a layer instance:
Event | Params | Description |
---|---|---|
pm:dragstart | e | Fired when a layer starts being dragged. |
pm:drag | e | Fired when a layer is dragged. |
pm:dragend | e | Fired when a layer stops being dragged. |
You can also listen to specific drag mode events on the map instance like this:
1map.on('pm:globaldrawmodetoggled', e => { 2 console.log(e); 3});
The event has an object with an enabled boolean and a reference to the map.
1// toggle drag mode like this: 2map.pm.toggleGlobalRemovalMode();
The following methods are available on map.pm
:
Method | Returns | Description |
---|---|---|
toggleGlobalRemovalMode() | - | Toggles global removal mode. |
globalRemovalEnabled() | Boolean | Returns true if global removal mode is enabled. false when disabled. |
The following events are available on a map instance:
Event | Params | Description |
---|---|---|
pm:remove | e | Fired when a layer is removed via Removal Mode |
layerremove | e | Standard Leaflet event. Fired when any layer is removed. |
You can also listen to specific removal mode events on the map instance like this:
1map.on('pm:globalremovalmodetoggled', e => { 2 console.log(e); 3});
The event has an object with an enabled boolean and a reference to the map.
Enable drawing for the shape "Cut" to draw a polygon that gets subtracted from all underlying polygons. This way you can create holes, cut polygons in half or remove parts of it.
Important: the cutted layer will be replaced, not updated. Listen to the
pm:cut
event to update your layer references in your code. The pm:cut
event
will provide you with the original layer and returns the resulting
layer(s) that is/are added to the map as a Polygon or MultiPolygon.
1// enable cutting mode
2map.pm.Draw.Cut.enable({
3 allowSelfIntersection: false,
4});
Available options are the same as in drawing mode.
You can use these methods on map.pm.Draw.Cut
to handle Cutting mode:
Method | Returns | Description |
---|---|---|
enable(options ) | - | Enable Cutting Mode. |
disable() | - | Disable Cutting Mode |
toggle(options ) | - | Toggle Cutting Mode |
The following events are available on a layer instance:
Event | Params | Description |
---|---|---|
pm:cut | e | Fired when the layer being cut |
The following events are available on a map instance:
Event | Params | Description |
---|---|---|
pm:cut | e | Fired when any layer is being cut |
Change the language of user-facing copy in leaflet.pm
1map.pm.setLang('de');
Currently available languages are en
, de
, it
, ru
, ro
, es
, fr
, pt_br
, zh
and nl
.
To add translations to the plugin, you can add a translation file via Pull Request.
You can also provide your own custom translations.
1const customTranslation = { 2 tooltips: { 3 placeMarker: 'Custom Marker Translation', 4 }, 5}; 6 7map.pm.setLang('customName', customTranslation, 'en');
The 3rd parameter is the fallback language in case you only want to override a few Strings. See the english translation file for all available strings.
In order to change the style of the lines during draw, pass these options to the
enableDraw()
function.
1// optional options for line style during draw. These are the defaults 2var options = { 3 // the lines between coordinates/markers 4 templineStyle: { 5 color: 'red', 6 }, 7 8 // the line from the last marker to the mouse cursor 9 hintlineStyle: { 10 color: 'red', 11 dashArray: [5, 5], 12 }, 13}; 14 15// enable drawing mode for shape - e.g. Poly, Line, Circle, etc 16map.pm.enableDraw('Polygon', options);
To customize the style of the drawn layer with leaflet options, you can either
pass the options to enableDraw
:
1// optional options for line style during draw. These are the defaults 2var options = { 3 templineStyle: {}, 4 hintlineStyle: {}, 5 pathOptions: { 6 // add leaflet options for polylines/polygons 7 color: 'orange', 8 fillColor: 'green', 9 }, 10}; 11 12// enable drawing mode for shape - e.g. Poly or Line 13map.pm.enableDraw('Polygon', options);
or set the options generally:
1map.pm.setPathOptions({
2 color: 'orange',
3 fillColor: 'green',
4 fillOpacity: 0.4,
5});
I'm adopting the Issue Management of lodash which means, feature requests get the "Feature Request" Label and then get closed. You can upvote existing feature requests (or create new ones). Upvotes make me see how much a feature is requested and prioritize their implementation. Please see the existing Feature Requests here and upvote if you want them to be implemented.
As I never built a leaflet plugin before, I looked heavily into the code of leaflet.draw to find out how to do stuff. So don't be surprised to see some familiar code.
I also took a hard look at the great L.GeometryUtil for some of my helper functions.
If you want to support the development of leaflet.pm, consider subscribing to the services of Geoman.
No vulnerabilities found.
No security vulnerabilities found.