geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
https://geoman.io
MIT License
2.21k stars 433 forks source link

Put existing Line object into draw mode (so we can continue drawing it) #294

Closed jmoe closed 6 years ago

jmoe commented 6 years ago

Would like to toggle the draw mode on an existing line object. Like toggleEdit expect putting it into draw mode with snapping and the cursor and hint line, etc.. Is this possible?

codeofsumit commented 6 years ago

Hi @jmoe, no that's not possible right now. I'll mark it as a feature request and close the issue. If more upvotes are on this I will consider implementing it.

Falke-Design commented 3 years ago

Function for L.PM.Edit.Line:

  continueDrawing(){
    if(!this._map){
      this._map = this._layer._map;
    }
    this._map.pm.enableDraw(this._shape);

    this._layer.getLatLngs().flat(999).forEach((latlng)=>{
      this._map.pm.Draw[this._shape]._createVertex({latlng});
    });
    this._layer.remove();
  }

But a few things:

lmachens commented 2 years ago

This would be useful for me too, or is there another option to continue drawing a line?

ABPW10 commented 1 year ago

This would be useful for me too, I'd like to upvote this request, or is there another option to continue drawing a line?

FrancisBaileyH commented 5 months ago

If anyone needs a quick fix for this I managed to plug into the existing event system to return a layer into draw mode. In my case I was targeting the polyline shape, but you can modify it to your needs:

// Listen for the Draw Line event
this.map.on("pm:drawstart", e => {
    if (e.shape == "Line") {
        // Find the first layer that was a line and also drawn by geoman
        // This code assumes you only want one line drawn
        // if you had multiple lines, you'll need to pass in the layer you're attempting to renenable
        var existingLine = this.map.pm.getGeomanDrawLayers().find(layer => {
            return layer.pm._shape == "Line"
        });
        if (existingLine !== undefined) {
            // set the templayer as done in L.Draw.Line
            existingLine._pmTempLayer = true;
            // set the working layer to our existing layer
            this.map.pm.Draw.Line._layer = existingLine;
            existingLine.getLatLngs().forEach(latLng => this.map.pm.Draw.Line._createMarker(latLng));
            // When geoman finishes drawing a line it will create a *new* polyline and add it to the map
            // our existing layer will still exist on the map on top of the new polyline that geoman just added
            // so add an event listener to remove our existing layer when we're done drawing to prevent duplicates
            this.map.once("pm:drawend", finishEvent => {
                this.map.removeLayer(existingLine);
            });
        }
    }
});

Working for the latest leaflet version: 2.1.5. Note that it relies on "private" properties and those can be subject to change.