Leaflet

an open-source JavaScript library
for mobile-friendly interactive maps

← Back to the list of blog posts

Leaflet.draw 0.2 Released

This is a guest post from Jacob Toye, an active Leaflet contributor and also the author of the most sophisticated vector drawing and editing plugin out there, which is presented in this post.

Leaflet.draw was born from the need to provide users with the ability draw polygons on the map. Leaflet already provided a very nice way of editing existing polylines and polygons. The logical next step was to expand on this functionality to allow the creation of these layers, and ultimately the other vector layers.

Upon release the immediate response from the Leaflet community was very positive. It became clear that the next step would be progressing this tool to a state where users could edit and delete shapes in addition to creating them. This is ultimately what Leaflet.draw 0.2 set out to do.

After a few months of off and on development, with most of this spare time kindly sponsored by my employer Smartrak, we proudly present Leaflet.draw 0.2 – your one stop plugin for drawing, editing and deleting vectors and markers on Leaflet maps. :)

Note from Vladimir: the polyline/polygon editing functionality from Leaflet core has been moved into this plugin where it fits much better. The plugin in turn has moved into Leaflet organization on GitHub and is now officially supported by the Leaflet development team. Note that version 0.2 currently depends on Leaflet master (in-progress development version) to work.

You can download the latest version from the github repo. Please report any bugs you come across on the issues page.

Features

Leaflet.draw is designed to not only be easy for end users to use, but also for developers to integrate.

How to use

Leaflet.draw is very simple to drop into you Leaflet application. The following example will add both the draw and edit toolbars to a map:

// create a map in the "map" div, set the view to a given place and zoom
var map = L.map('map').setView([175.30867, -37.77914], 13);

// add an OpenStreetMap tile layer
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
	attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

// Initialize the FeatureGroup to store editable layers
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);

// Initialize the draw control and pass it the FeatureGroup of editable layers
var drawControl = new L.Control.Draw({
	edit: {
		featureGroup: drawnItems
	}
});
map.addControl(drawControl);

Handling newly created layers

Once you have successfully added the Leaflet.draw plugin your map you will want to respond to the different actions users can trigger.

map.on('draw:created', function (e) {
	var type = e.layerType,
		layer = e.layer;

	if (type === 'marker') {
		// Do marker specific actions
	}

	// Do whatever else you need to. (save to db, add to map etc)
	drawnItems.addLayer(layer);
});

map.on('draw:edited', function () {
	// Update db to save latest changes.
});

map.on('draw:deleted', function () {
	// Update db to save latest changes.
});

See the Leaflet.draw README for more details on how to configure the plugin.

Thanks

First and foremost I would like to thank my employer Smartrak. Without their attitude to open source software I would not have had the time to complete this plugin.

The Leaflet developer community have been great in supporting this plugin through inspiration, pull requests and issue reports. Special thanks to: @mourner, @danzel, @brunob, @tnightingale, @Starefossen, and @shramov.

Closing

I’ve had a great time implementing this plugin. I hope you enjoy using it. If you have a question or just want to say hi, send me an email at jacob.toye@gmail.com.

Cheers, Jacob Toye