Skip to main content

Edit Mode

Edit Mode

You can enable Edit Mode for all layers on a map like this:

// enable global Edit Mode
map.pm.enableGlobalEditMode(options);

The following methods are available on map.pm:

MethodReturnsDescription
enableGlobalEditMode(options)-Enables global Edit Mode.
disableGlobalEditMode()-Disables global Edit Mode.
toggleGlobalEditMode(options)-Toggles global Edit Mode.
globalEditModeEnabled()BooleanReturns true if global Edit Mode is enabled. false when disabled.

Enable Edit Mode only for one layer:

// enable Edit Mode
layer.pm.enable({
allowSelfIntersection: false,
});

The following methods are available for layers under layer.pm:

MethodReturnsDescription
enable(options)-Enables Edit Mode. The passed options are preserved, even when the mode is enabled via the Toolbar. options is optional.
disable()-Disables Edit Mode.
toggleEdit(options)-Toggles Edit Mode. Passed options are preserved. options is optional.
enabled()BooleanReturns true if Edit Mode is enabled. false when disabled.
hasSelfIntersection()BooleanReturns true if Line or Polygon has a self intersection.
remove()-Removes the layer with the same checks as GlobalRemovalMode.
getShape()StringReturns the shape of the layer.
setOptions(options)-Set the options on the layer.
getOptions()ObjectReturns the options of the layer.

Edit Mode Options

See the available options in the table below.

OptionDefaultDescription
snappabletrueEnable snapping to other layers vertices for precision drawing. Can be disabled by holding the ALT key.
snapDistance20The distance to another vertex when a snap should happen.
snapMiddlefalseAllow snapping in the middle of two vertices (middleMarker).
snapSegmenttrueAllow snapping between two vertices.
snapVertextrueAllow snapping to vertices.
allowSelfIntersectiontrueAllow/Disallow self-intersections on Polygons and Polylines.
allowSelfIntersectionEditfalseAllow/Disallow to change vertices they are connected to a intersecting line. Only working if allowSelfIntersection is true and the layer is already self-intersecting while enabling Edit Mode.
preventMarkerRemovalfalseDisable the removal of markers/vertexes via right click.
removeLayerBelowMinVertexCounttrueIf true, vertex removal that cause a layer to fall below their minimum required vertices will remove the entire layer. If false, these vertices can't be removed. Minimum vertices are 2 for Lines and 3 for Polygons.
syncLayersOnDragfalseDefines which layers should dragged with this layer together. true syncs all layers in the same LayerGroup(s) or you pass an Array of layers to sync.
allowEditingtrueEdit-Mode for the layer can disabled (pm.enable()).
allowRemovaltrueRemoving can be disabled for the layer.
allowCuttingtrueLayer can be prevented from cutting.
allowRotationtrueLayer can be prevented from rotation.
draggabletrueDragging can be disabled for the layer.
addVertexOnclickLeaflet layer event to add a vertex to a Line or Polygon, like 'dblclick'. Here's a list.
addVertexValidationundefinedA function for validation if a vertex (of a Line / Polygon) is allowed to add. It passes a object with [layer, marker, event}. For example to check if the layer has a certain property or if the Ctrl key is pressed.
removeVertexOncontextmenuLeaflet layer event to remove a vertex from a Line or Polygon, like 'dblclick'. Here's a list.
removeVertexValidationundefinedA function for validation if a vertex (of a Line / Polygon) is allowed to remove. It passes a object with [layer, marker, event}. For example to check if the layer has a certain property or if the Ctrl key is pressed.
moveVertexValidationundefinedA function for validation if a vertex / helper-marker is allowed to move / drag. It passes a object with [layer, marker, event}. For example to check if the layer has a certain property or if the Ctrl key is pressed.
limitMarkersToCount-1Shows only n markers closest to the cursor. Use -1 for no limit.
limitMarkersToZoom-1Shows markers when under the given zoom level. ⭐
limitMarkersToViewportfalseShows only markers in the viewport. ⭐
limitMarkersToClickfalseShows markers only after the layer was clicked. ⭐
pinningfalsePin shared vertices/markers together during edit Details. ⭐
allowPinningtrueLayer can be prevented from pinning.⭐
allowScaletrueLayer can be prevented from scaling.⭐
centerScalingtrueScale origin is the center, else it is the opposite corner. If false Alt-Key can be used. Scale Mode. ⭐
uniformScalingtrueWidth and height are scaled with the same ratio. If false Shift-Key can be used. Scale Mode. ⭐
allowAutoTracingtrueLayer can be prevented from auto tracing.⭐
addVertexOnClickfalseAdd Vertices while clicking on the line of Polyline or Polygon⭐

You can listen to events related to editing on events like this:

// listen to when a layer is changed in Edit Mode
layer.on("pm:edit", (e) => {
console.log(e);
});

The following events are available on a layer instance:

EventParamsDescriptionOutput
pm:editeFired when a layer is edited.layer, shape
pm:updateeFired when Edit Mode is disabled and a layer is edited and its coordinates have changed.layer, shape
pm:enableeFired when Edit Mode on a layer is enabled.layer, shape
pm:disableeFired when Edit Mode on a layer is disabled.layer, shape
pm:vertexaddedeFired when a vertex is added.layer, indexPath, latlng, marker, shape
pm:vertexremovedeFired when a vertex is removed.layer, indexPath, marker, shape
pm:vertexclickeFired when a vertex is clicked.layer, indexPath, markerEvent, shape
pm:markerdragstarteFired when dragging of a marker which corresponds to a vertex starts.layer, indexPath, markerEvent, shape
pm:markerdrageFired when dragging a vertex-marker.layer, indexPath, markerEvent, shape
pm:markerdragendeFired when dragging of a vertex-marker ends.layer, indexPath, markerEvent, shape, intersectionReset
pm:layerreseteFired when coords of a layer are reset. E.g. by self-intersection.layer, indexPath, markerEvent, shape
pm:snapdrageFired during a marker move/drag. Payload includes info about involved layers and snapping calculation.shape, distance, layer = workingLayer, marker, layerInteractedWith, segment, snapLatLng
pm:snapeFired when a vertex-marker is snapped to another vertex. Also fired on the marker itself.shape, distance, layer = workingLayer, marker, layerInteractedWith, segment, snapLatLng
pm:unsnapeFired when a vertex-marker is unsnapped from a vertex. Also fired on the marker itself.shape, distance, layer = workingLayer, marker, layerInteractedWith, segment, snapLatLng
pm:intersecteWhen allowSelfIntersection: false, this event is fired as soon as a self-intersection is detected.layer, intersection, shape
pm:centerplacedeFired when the center of a circle is moved.layer, latlng, shape
pm:changeeFired coordinates of the layer changed.layer, latlngs, shape

The following events are available on a map instance:

EventParamsDescriptionOutput
pm:globaleditmodetoggledeFired when Edit Mode is toggled.enabled, map

You can also listen to specific Edit Mode events on the map instance like this:

map.on("pm:globaleditmodetoggled", (e) => {
console.log(e);
});

Example

Loading...