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

前端 未结 3 486
无人共我
无人共我 2020-12-04 14:13

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 mana

3条回答
  •  爱一瞬间的悲伤
    2020-12-04 14:58

    I think it's worth mentioning Jacob Toyes answer to this problem. You're always drawing with handlers in leaflet.draw - not directly with layers. If you want to edit a layer, you use the handler saved in a layers editing field like that: layer.editing.enable();. And if you want to create a new layer, you first create a new handler:

    // Define you draw handler somewhere where you click handler can access it. N.B. pass any draw options into the handler
    var polygonDrawer = new L.Draw.Polyline(map);
    
    // Assumming you have a Leaflet map accessible
    map.on('draw:created', function (e) {
        var type = e.layerType,
            layer = e.layer;
    
        // Do whatever you want with the layer.
        // e.type will be the type of layer that has been draw (polyline, marker, polygon, rectangle, circle)
        // E.g. add it to the map
        layer.addTo(map);
    });
    
    // Click handler for you button to start drawing polygons
    $('#draw_poly').click(function() {
        polygonDrawer.enable();
    });
    

    By now there actually is an example on the leaflet.draw github page: https://github.com/Leaflet/Leaflet.draw/blob/develop/docs/examples/edithandlers.html

    Nevertheless I think handlers aren't well documented there yet.

    Like stated above, L.EditToolbar.Edit and L.EditToolbar.Delete expose interesting methods and events like editstart and editstop. What's not mentioned is that these two classes themselves are derived from L.Handler.

提交回复
热议问题