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:
- Created 9 years ago
- Comments:16 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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