How to make features drawn with leaflet.draw always snap to other features dynamically drawn?
See original GitHub issueHi,
during drawing with leaflet.draw, the features will snap. But if they are drawn and made editable they won’t. How to solve this? Do I really have to manage all objects to refresh the guideLayers to make them snapping again?
// Create a map in the "map" div, set the view to a given place and zoom
var map = L.map('map').setView([48.48922, 1.40033], 13);
// Add scale
L.control.scale().addTo(map);
// Add an OpenStreetMap tile layer
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Initialise the FeatureGroup to store editable layers
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
// Snapping
guideLayers = new Array();
// Initialise the draw control and pass it the FeatureGroup of editable layers
var drawControl = new L.Control.Draw({
draw: {
polyline: { guideLayers: guideLayers },
marker: { guideLayers: guideLayers },
polygon: false,
rectangle: false,
circle: false
},
edit: {
featureGroup: drawnItems,
edit: false
// Delete still remains useable
}
});
map.addControl(drawControl);
map.on('draw:created', function (event) {
var type = event.layerType;
var layer = event.layer;
if(type === 'marker') {
// Make marker draggable
layer.options.draggable = true;
}
else {
// Make line editable
layer.editing.enable();
// Activate snapping - does not work ... :(
layer.snapediting = new L.Handler.PolylineSnap(map,layer);
layer.snapediting.addGuideLayer(guideLayers);
layer.snapediting.enable();
}
// Add to drawnItems
drawnItems.addLayer(layer);
// Add newly drawn feature to list of snappable features
guideLayers.push(layer);
});
Tried it with adding a for-loop at the end of the draw:created event but sadly it doesn’t work
for(var i = 0;i < guideLayers.length;i++) {
console.log('layer ' + i + ' refreshed');
guideLayers[i].snapediting.addGuideLayer(guideLayers);
}
Thanks
Issue Analytics
- State:
- Created 10 years ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
How to initiate the draw function without toolbar? - Stack ...
I want to initiate drawing a polygon without using the toolbar from leaflet.draw . I've managed to find the property that allows editing...
Read more >Leaflet Draw Documentation
This tells the plugin which FeatureGroup contains the layers that should be editable. The featureGroup can contain 0 or more features with geometry...
Read more >Documentation - a JavaScript library for interactive maps
To set the restriction dynamically, use setMaxBounds method. renderer, Renderer, *, The default method for drawing vector layers on the map.
Read more >How to highlight marker dynamically with leaflet and leaflet draw
I am trying to make a selection via drawing a rectangle (or any shape) with leaflet draw. The selected features should be highlighted....
Read more >4.6 Drawing & User Editing | Mapping in LeafletJS - YouTube
Mapster has decided to make all its courses, content, and code free from now on! Enjoy this free course on how to use...
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
Hi to all, I have had the same problem and found solutio using L.Draw. There a few things which have to be observed in next code:
The main hint to solve mentionde problem was line 4: layer.addTo(map);. To be able to snap with already existing layers ( when we Edit newly created layer) we have to add him to map. The other lines are important to but were metioned earlire on current topic.
P.S. the rest of code, abow code metionde earlier, was:
i think #30 resolve this