Leaflet.draw Documentation

repository·develop·Indexed 24 days ago

https://github.com/leaflet/leaflet.draw

A vector drawing plugin for Leaflet maps that adds support for drawing and editing vector layers, including polylines, polygons, rectangles, circles, and markers. It includes the L.Control.Draw interface for toolbar configuration, L.drawLocal for language customization, and support for handling shape creation via L.Draw.Event.CREATED.

Tokens
1.8K
Snippets
5
Records
8
Agent score
35%

What's inside Leaflet.draw

  1. Upgrade from Leaflet.draw 0.1 to 0.2: Use consolidated CREATED events

    develop

    Leaflet.draw 0.2 consolidated individual shape creation events (like draw:poly-created, draw:rectangle-created, etc.) into a single L.Draw.Event.CREATED event.

    To handle new shapes, listen for L.Draw.Event.CREATED. You can determine which shape was created by checking e.layerType and access the actual layer via e.layer.

    map.on(L.Draw.Event.CREATED, function (e) {
    	var type = e.layerType,
    		layer = e.layer;
    
    	if (type === 'marker') {
    		// Do any marker specific logic here
    	}
    
    	map.addLayer(layer);
    });
  2. Customize language and text in Leaflet.draw

    develop

    Leaflet.draw uses the L.drawLocal configuration object to manage all user-facing text, such as button titles and tooltips. You can override these strings to support different languages or custom messaging.

    To customize text, modify the properties within L.drawLocal. For example, you can change a toolbar button's title or a specific handler's tooltip text.

    // Set the button title text for the polygon button
    L.drawLocal.draw.toolbar.buttons.polygon = 'Draw a sexy polygon!';
    
    // Set the tooltip start text for the rectangle
    L.drawLocal.draw.handlers.rectangle.tooltip.start = 'Not telling...';
  3. Upgrade from Leaflet.draw 0.1 to 0.2: CSS changes

    develop
    Version 0.2 introduced significant changes to the CSS. If you have implemented custom styles for Leaflet.draw, you should review the leaflet.draw.css file to ensure your customizations are still compatible or to update them to the new structure.
  4. Configure L.Control.Draw options

    develop

    The L.Control.Draw constructor accepts an options object to define the behavior and appearance of the drawing interface.

    Key configuration areas include:

    • position: The position of the toolbar on the map (e.g., 'topright').
    • draw: Configuration for specific drawing tools (polyline, polygon, circle, rectangle, marker).
      • shapeOptions: Styles for the drawn vector layers (color, weight, etc.).
      • allowIntersection: Boolean to restrict polygons to simple shapes.
      • drawError: Configuration for error messages and colors when drawing constraints are violated.
      • icon: Custom icon for the marker tool.
      • Setting a tool to false (e.g., circle: false) disables that specific drawing tool.
    • edit: Configuration for the editing interface.
      • featureGroup: REQUIRED. The L.FeatureGroup containing the layers that should be editable.
      • remove: Boolean to enable/disable the delete functionality.
    var editableLayers = new L.FeatureGroup();
    map.addLayer(editableLayers);
    
    var MyCustomMarker = L.Icon.extend({
        options: {
            shadowUrl: null,
            iconAnchor: new L.Point(12, 12),
            iconSize: new L.Point(24, 24),
            iconUrl: 'link/to/image.png'
        }
    });
    
    var options = {
        position: 'topright',
        draw: {
            polyline: {
                shapeOptions: {
                    color: '#f357a1',
                    weight: 10
                }
            },
            polygon: {
                allowIntersection: false, // Restricts shapes to simple polygons
                drawError: {
                    color: '#e1e100', // Color the shape will turn when intersects
                    message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect
                },
                shapeOptions: {
                    color: '#bada55'
                }
            },
            circle: false, // Turns off this drawing tool
            rectangle: {
                shapeOptions: {
                    clickable: false
                }
            },
            marker: {
                icon: new MyCustomMarker()
            }
        },
        edit: {
            featureGroup: editableLayers, //REQUIRED!!
            remove: false
        }
    };
    
    var drawControl = new L.Control.Draw(options);
    map.addControl(drawControl);
    
    map.on(L.Draw.Event.CREATED, function (e) {
        var type = e.layerType,
            layer = e.layer;
    
        if (type === 'marker') {
            layer.bindPopup('A popup!');
        }
    
        editableLayers.addLayer(layer);
    });
  5. Update drawing handler options dynamically

    develop

    If you need to change the configuration of drawing tools after the L.Control.Draw instance has been initialized, use the setDrawingOptions method. This allows you to update styles or constraints on the fly.

    drawControl.setDrawingOptions({
        rectangle: {
        	shapeOptions: {
        		color: '#0000FF'
        	}
        }
    });
  6. Upgrade from Leaflet.draw 0.1 to 0.2: Updated drawing start/stop events

    develop

    The events for starting and stopping a drawing handler have been renamed to align with the standard event naming convention. Additionally, the property used to identify the drawing type in the event arguments has changed.

    Old EventNew Event
    drawingdraw:drawstart
    drawing-disableddraw:drawstop

    Argument Change:

    • The property drawingType has been renamed to layerType in the event arguments.