Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet.draw mapping: How to initiate the draw function without toolbar?

For anyone experienced with leaflet or leaflet.draw plugin:

I want to initiate drawing a polygon without using the toolbar from leaflet.draw. I've managed to find the property that allows editing without using the toolbar (layer.editing.enable();) by searching online (it's not in the main documentation). I can't seem to find how to begin drawing a polygon without the toolbar button. Any help would be much appreciated!

Thank you :)

like image 926
Sam Avatar asked Apr 02 '13 21:04

Sam


People also ask

How do you use a leaflet draw plugin?

To use the Leaflet. draw plugin, you need to add the js and css files to your html header, like so: <! DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!


1 Answers

This simple code works for me:

new L.Draw.Polyline(map, drawControl.options.polyline).enable(); 

Just put it into the onclick handler of your custom button (or wherever you want).

The variables map and drawControl are references to your leaflet map and draw control.

Diving into the source code (leaflet.draw-src.js) you can find the functions to draw the other elements and to edit or delete them.

new L.Draw.Polygon(map, drawControl.options.polygon).enable() new L.Draw.Rectangle(map, drawControl.options.rectangle).enable() new L.Draw.Circle(map, drawControl.options.circle).enable() new L.Draw.Marker(map, drawControl.options.marker).enable()  new L.EditToolbar.Edit(map, {                 featureGroup: drawControl.options.featureGroup,                 selectedPathOptions: drawControl.options.edit.selectedPathOptions             }) new L.EditToolbar.Delete(map, {                 featureGroup: drawControl.options.featureGroup             }) 

I hope this will be useful for you too.

EDIT: The L.EditToolbar.Edit and L.EditToolbar.Delete classes expose the following useful methods:

  • enable(): to start edit/delete mode
  • disable(): to return to standard mode
  • save(): to save changes (it fires draw:edited / draw:deleted events)
  • revertLayers(): to undo changes
like image 97
BaCH Avatar answered Sep 19 '22 11:09

BaCH