Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

leaflet-draw

Leaflet651.6kMIT1.0.4TypeScript support: definitely-typed

Vector drawing plugin for Leaflet

maps, leaflet, client, vector, drawing, draw

readme

GitHub version npm version NPM Downloads Bower version Build Status Leaflet.draw Chat GitHub issues GitHub forks GitHub stars GitHub license

Leaflet.draw

Adds support for drawing and editing vectors and markers on Leaflet maps.

Supports Leaflet 0.7.x and 1.0.0+ branches.

Please check out our Api Documentation

Upgrading from Leaflet.draw 0.1

Leaflet.draw 0.2.0 changes a LOT of things from 0.1. Please see BREAKING CHANGES for how to upgrade.

In this readme

Customizing language and text in Leaflet.draw

Leaflet.draw uses the L.drawLocal configuration object to set any text used in the plugin. Customizing this will allow support for changing the text or supporting another language.

See Leaflet.draw.js for the default strings.

E.g.

    // 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...';

Common tasks

The following examples outline some common tasks.

Example Leaflet.draw config

The following example will show you how to:

  1. Change the position of the control's toolbar.
  2. Customize the styles of a vector layer.
  3. Use a custom marker.
  4. Disable the delete functionality.
    var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
        cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18}),
        map = new L.Map('map', {layers: [cloudmade], center: new L.LatLng(-37.7772, 175.2756), zoom: 15 });

    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);
    });

Changing a drawing handlers options

You can change a draw handlers options after initialisation by using the setDrawingOptions method on the Leaflet.draw control.

E.g. to change the colour of the rectangle:

drawControl.setDrawingOptions({
    rectangle: {
        shapeOptions: {
            color: '#0000FF'
        }
    }
});

Contributing

Testing

To test you can install the npm dependencies:

npm install

and then use:

jake test

Documentation

Documentation is build with Leafdoc, to generate the documentation use

jake docs

and the generated html documentation is saved to ./docs/leaflet-draw-latest.html

Thanks

Touch friendly version of Leaflet.draw was created by Michael Guild (https://github.com/michaelguild13).

The touch support was initiated due to a demand for it at National Geographic for their Map Maker Projected (http://mapmaker.education.nationalgeographic.com/) that was created by Michael Guild and Daniel Schep (https://github.com/dschep)

Thanks so much to @brunob, @tnightingale, and @shramov. I got a lot of ideas from their Leaflet plugins.

All the contributors and issue reporters of this plugin rock. Thanks for tidying up my mess and keeping the plugin on track.

The icons used for some of the toolbar buttons are either from http://glyphicons.com/ or inspired by them. <3 Glyphicons!

Finally, @mourner is the man! Thanks for dedicating so much of your time to create the gosh darn best JavaScript mapping library around.

changelog

Leaflet.draw Changelog

master

An in-progress version being developed on the master branch.

0.4.10 (July 3, 2017)

  • Locking Leaflet version to 0.7.x-1.0.x
  • Polygon line length, square kilometers and configurable precision
  • fix for _getMeasurementString
  • Locale number formatting added

0.4.2 (November 7, 2016)

Improvements

  • Documentation is now automatically generated
  • L.Draw.Event is new, has all events in string format

Bugfixes

  • Travis tests

0.4.1 (October 21, 2016)

Bugfixes

  • fix markers becomming invisible when in edit mode
  • Fix linking for EditPolyOptions
  • Fixes very minor issue in readme with invalid variable name

0.4.0 (October 13, 2016)

Improvements

  • Add support for Leaflet 1.0.0+

Bugfixes

  • New L.Draw.Tooltip to mitigate L.Tooltip collision

Potential Issues

  • Several namespace changes, see BREAKINGCHANGES.md
  • Cross support for both 0.7.0 and 1.0.0

0.3.0 (March 09, 2016)

Improvements

  • Add support for touch devices
  • Corrected license
  • Linter

Bugfixes

  • Added mouse handlers
  • Fixed event listener leaks in Polyline
  • Edit vertex event

0.2.4 (February 04, 2014)

Improvements

  • Add support for toolbar touch styles. (by @alanshaw). #269
  • Add support for maintaining a layers color when entering edit mode.
  • Add support for showing area when drawing a rectangle.
  • Refactor marker editing to bring in line with path editing handlers. Decouple setting editing style from edit toolbar. (by @manleyjster). #323
  • Prevent skewing on selected edit marker. (by @kyletolle). #341
  • Add support for changing the 'Radius' label text.

Bugfixes

  • Fix deleted layers LayerGroup constructor type. (by @w8r ). #334

0.2.3 (January 14, 2014)

Improvements

  • Restrict editing polygons so that at least 3 points are present. (by @Zverik). #200
  • Tooltips initially start hidden until the mouse has been moved. (by @Zverik). #210
  • Fixup spelling errors. (by @nexushoratio). #223
  • Combine ie specific style within leaflet.draw.css stylesheet. (by @frankrowe). #221
  • Improve my terrible engrish. (by @erictheise). #237
  • Fire editstart events when a poly or simple shape is initially edited. (by @atombender). #245
  • Add ability to add a new vertex to a polyline or polygon hander.
  • Added ability to remove/undo the last placed point for polyline or polygons. (by @Zverik). #242
  • Dynamically position the action toolbars. (by @DevinClark). #240
  • Improve polyline/polygon drawing by accepting some motion on click. (by @atombender). #249
  • Only draw a limited number of guide dashes to improve performance in some rare cases. #254

Bugfixes

  • Fix edit toolbar so diabled state is represented correctly. (by @joeybaker). #203
  • Fixed path middle marker positions. (by @Zverik). #208
  • Fix issue where toolbar buttons would have focus after clicked so couldn't use escape to cancel until clicked map at least once.
  • Fix toolbar icons for retina displays. (by @dwnoble). #217
  • Ensure that options are not shared between draw handler classes. (by @yohanboniface). #219
  • Fix bug where multiple handlers could be active. (by @manubb). #247

0.2.2 (October 4, 2013)

Improvements

  • Refactored the L.drawLocal' object to be better structured and use this object whereever text is used. *NOTE: THIS IS A NEW FORMAT, SO WILL BRESK ANY EXISTING CUSTOML.drawLocal` SETTINGS*.
  • Added Imperial measurements to compliment the existing Metric measurements when drawing a polyline or polygon.
  • Added draw:editstart and draw:editstop events. (by @bhell). #175
  • Added repeatMode option that will allow repeated drawing of features. (by @jayhogan and @cscheid). #178
  • Added abilit to set circle radius measurement to imperial units.
  • Added disabled state for edit/delete buttons when no layers present. (inspired by @snkashis). #136
  • Add showLength and showRadius options to circle and polyline. (by @Zverik). #195
  • Add option to disable tooltips. (by @Zverik). #196

Bugfixes

  • Fixed bug where edit handlers could not be disabled.
  • Added support for displaying the toolbar on the right hand side of the map. (by @paulcpederson). #164
  • Add flexible width action buttons. (by @Grsmto). #181
  • Check for icon existence before disabling edit state. (by @tmcw). #182
  • Only update guideslines when guidelines are present. (by @jayhogan). #188
  • Fixes to localization code so it can be correctly set after files have been loaded.
  • Fix for firing draw:edit twice for Draw.SimpleShape. (by @cazacugmihai). #192
  • Fix last edit menu buttons from wrapping. (by @moiarcsan). #198

0.2.1 (July 5, 2013)

Improvements

  • draw:edited now returns a FeatureGroup of features edited. (by @jmkelly). #95
  • Circle tooltip shows the radius (in m) while drawing.
  • Added Leaflet version check to inform developers that Leaflet 0.6+ is required.
  • Added ability to finish drawing polygons by double clicking. (inspired by @snkashis). #121
  • Added test environment. (by @iirvine). #123
  • Added L.drawLocal object to allow users to customize the text used in the plugin. Addresses localization issues. (by @Starefossen). #87
  • Added ability to disable edit mode path and marker styles. (inspired by @markgibbons25). #121
  • Added area calculation when drawing a polygon.
  • Polyline and Polygon tooltips update on click as well as mouse move.

Bugfixes

  • Fixed issue where removing a vertex or adding a new one via midpoints would not update the edited state for polylines and polygons.
  • Fixed issue where not passing in the context to off() would result in the event from not being unbound.(by @koppelbakje). #95
  • Fixed issue where removing the draw control from the map would result in an error.
  • Fixed bug where removing points created by dragging midpoints would cause the polyline to not reflect any newly created points.
  • Fixed regression where handlers were not able to be disabled.(by @yohanboniface). #139
  • Fixed bug where L.Draw.Polyline would try to remove a non-existant handler if the user cancelled and the polyline only had a single point.

0.2.0 (February 20, 2013)

Major new version. Added Edit toolbar which allows editing and deleting shapes.

Features

  • Consistant event for shape creation. (by @krikrou). #58

Bugfixes

0.1.7 (February 11, 2013)

  • Add sanity check for toolbar buttons when adding top and bottom classes. (by @yohanboniface). #60

0.1.6 (January 17, 2013)

  • Updated toolbar styles to be in line with the new Leaflet zoom in/out styles.

0.1.5 (December 10, 2012)

Features

  • Added 'drawing-disabled' event fired on the map when a draw handler is disabled. (by @ajbeaven). #35
  • Added 'drawing' event fired on the map when a draw handler is actived. (by @ajbeaven). #30

Bugfixes

  • Stopped L.Control.Draw from storing handlers in it's prototype. (by @thegreat). #37

0.1.4 (October 8, 2012)

Bugfixes

  • Fixed a bug that would cause an error when creating rectangles/circles withought moving the mouse. (by @inpursuit). #25
  • Fixed a bug that would cause an error when clicking a different drawing tool while another mode enabled. (by @thegreat). #27
  • Fixed control buttons breaking plugin in oldIE.
  • Fixed drawing polylines and polygons in oldIE.

0.1.3 (October 3, 2012)

Bugfixes

  • Tip label will now show over vertex markers.
  • Added ability to draw on top of existing markers and vector layers.
  • Clicking on a map object that has a click handler no longer triggers the click event when in drawing mode.

Pre-0.1.3

Check the commit history for changes previous to 0.1.3.