question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow user to draw only one shape

See original GitHub issue

[this issue has its own solution]

I have a site where I want the user to draw a shape. The shape’s GeoJSON description will be saved to a database for them to access the shape later, but I only want to allow them to create one shape per database record.

I didn’t see a sanctioned way so I tried a method that watched for the event “draw:created” and then disabled the drawing toolbar.

Use

map.on('draw:created', function (e) {
    console.log("something was created; hiding draw control");
    var type = e.layerType,
        layer = e.layer;

    drawControl.removeFrom(map);

    editableLayers.addLayer(layer);
});

to disable the draw control and

map.on('draw:deletestop', function(e) {
    console.log("something was deleted; showing draw control");
    var type = e.layerType,
        layer = e.layer;

    drawControl.addTo(map);
});

to enable the draw control.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
nk9commented, Jun 9, 2019

There are a number of improvements that have been made to the original solution over the years. Here is a complete, working solution with Leaflet Draw 1.0.4:

// Set up the edit control
// Only allow a single shape to be drawn
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);

var drawControlFull = new L.Control.Draw({
    draw: {
        polyline: false,
    },
    edit: {
        featureGroup: drawnItems
    }
});
var drawControlEditOnly = new L.Control.Draw({
    edit: {
        featureGroup: drawnItems
    },
    draw: false
});
map.addControl(drawControlFull);

map.on(L.Draw.Event.CREATED, function (e) {
    drawnItems.addLayer(e.layer);
    drawControlFull.remove(map);
    drawControlEditOnly.addTo(map)
});
map.on(L.Draw.Event.DELETED, function(e) {
   if (drawnItems.getLayers().length === 0){
        drawControlEditOnly.remove(map);
        drawControlFull.addTo(map);
    };
});
10reactions
MattFerrarocommented, Aug 23, 2014

Great fix! I adopted your change a bit so that the UI precludes you from adding more than one, but fluidly allows you to remove and try again if you aren’t happy with your shape

var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);

var drawControlFull = new L.Control.Draw({
    edit: {
        featureGroup: drawnItems
    },
    draw: {
        polyline: false
    }
});


var drawControlEditOnly = new L.Control.Draw({
    edit: {
        featureGroup: drawnItems
    },
    draw: false
});

map.addControl(drawControlFull);

map.on("draw:created", function (e) {
    var layer = e.layer;
    layer.addTo(drawnItems);
    drawControlFull.removeFrom(map);
    drawControlEditOnly.addTo(map)
});

map.on("draw:deleted", function(e) {
    drawControlEditOnly.removeFrom(map);
    drawControlFull.addTo(map);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to allow only one feature/polygon to be edited at a time ...
In your onEachFeature function you should store the feature that was clicked so you can enable/disable editing in the click handler.
Read more >
react-leaflet-draw-only-one-shape - CodeSandbox
CodeSandbox is an online editor tailored for web applications. ... kboul / react-leaflet-draw-only-one-shape / master.
Read more >
Drawing shapes with canvas - Web APIs | MDN
Drawing rectangles​​ Unlike SVG, <canvas> only supports two primitive shapes: rectangles and paths (lists of points connected by lines). All ...
Read more >
Leaflet Draw Documentation
This will allow map users to draw vectors and markers. ... One of: remove Triggered when the user has finished removing shapes (remove...
Read more >
Draw shapes - Windows apps - Microsoft Learn
A Path is the most versatile Shape because you can use it to define an arbitrary geometry. But with this versatility comes complexity....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found