Leaflet.draw
Leaflet.draw copied to clipboard
close shape on polyline tool
hi, is there anyone that knows how to invoke "completeShape()" on the polyline tool? currently i check for how many vertexes are created but can't seem to complete the polyline after 1 vertex is drawn:
map.on('draw:drawvertex', function(e) {
if (Object.keys(e.layers._layers).length > 1) {
// need to close polyline here
}
});
Not a fantastic answer here, but you can listen to the draw-start event to grab the current (new) shape and save it to a scope before your draw:vertex listener to fire the complete shape on the polyline.
This event really should include the current layer in the callback instead of the group of layers.
Hi Baikon, Did you get any workaround here to invoke "completeShape()" ? I need to close shape after 4 vertex is drawn
Hello @ddproxy, I'm experiencing the same issue. The difference is I want to draw polyline with provided LatLngs using polylineDrawer.addVertex(). When I added the last LatLng, I invoke the polylineDrawer.completeShape(), but it gives me a bug. My polyline is gone. Would you please help me with this? Thanks. Here I attached my code to give much clearer context.
this.mapInstance.polylineDrawer = new L.Draw.Polyline(this.mapInstance.map as unknown as L.DrawMap);
this.mapInstance.polylineDrawer.enable();
const points = res.map(p => L.latLng(p.lat, p.lng));
points.map(p => {
this.mapInstance.polylineDrawer.addVertex(p); // add vertex point
});
/* Here my line gone if I uncomment this */
// this.mapInstance.polylineDrawer.completeShape();
Hello @ddproxy, I'm experiencing the same issue. The difference is I want to draw polyline with provided LatLngs using
polylineDrawer.addVertex(). When I added the last LatLng, I invoke thepolylineDrawer.completeShape(), but it gives me a bug. My polyline is gone. Would you please help me with this? Thanks. Here I attached my code to give much clearer context.this.mapInstance.polylineDrawer = new L.Draw.Polyline(this.mapInstance.map as unknown as L.DrawMap); this.mapInstance.polylineDrawer.enable(); const points = res.map(p => L.latLng(p.lat, p.lng)); points.map(p => { this.mapInstance.polylineDrawer.addVertex(p); // add vertex point }); /* Here my line gone if I uncomment this */ // this.mapInstance.polylineDrawer.completeShape();
Sorry, this issue is already resolved. I invoked the completeShape() method after my draw events listener (e.g. draw:created, etc), and suddenly, this works as expected. Please just inform if anyone finds another case. Thank you.