Funn_Bobby
Funn_Bobby

Reputation: 715

Openlayers 3 remove drawn feature

I am adding "draw interaction" and drawing features on a map...

 webMapValues.drawObj = new ol.interaction.Draw({                                
                            features: features,
                            type: /** @type {ol.geom.GeometryType} */ (webMapValues.drawType)

                        });

I am catching the "drawend" event...

 webMapValues.drawObj.on('drawend', function (e) {                            

                        //Here I am saving my feature to a data base
                          and displaying the saved vector...
                        control.UpdateObjects(e, "Draw");

                        //Here I would like to remove the vector that
                          draw interaction placed and just display the 
                          vector from the db


                    });

I can not figure out how to remove the vector that ol.interaction.Draw has placed so that only the vector from the data base shows after I save it?

I guess 'drawend' is not the place to do this...but I'm not sure how to achieve the functionality I need then...

any help is greatly appreciated!!

Upvotes: 3

Views: 4257

Answers (3)

momargoh
momargoh

Reputation: 66

I achieved this by listening to the "addfeature" event to immediately remove the added feature if the feature has the key "removeMe"

  yourVectorSource.on("addfeature", (event) => {
    if (event.feature.get("removeMe")) {
      this.source.removeFeature(event.feature);
    }
  });
  webMapValues.drawObj.on('drawend', function (e) {        
    control.UpdateObjects(e, "Draw");
    
    //add removeMe key now!
    e.feature.set("removeMe",true);
  });

Upvotes: 0

Anabella G.
Anabella G.

Reputation: 41

function noDraw() {

  if (draw) {
        map.removeInteraction(webMapValues.drawObj);
  }


};

Upvotes: 0

Sumanth Shastry
Sumanth Shastry

Reputation: 1159

If you look at the code of ol.interaction.Draw() interactiondrawend is called before the feature gets inserted to the ol.source.

finishDrawing() method will insert the feature object.

Below is the snippet from the latest sourcecode

/** * Stop drawing and add the sketch feature to the target layer.

The {@link ol.interaction.DrawEventType.DRAWEND} event is dispatched before * inserting the feature. * @api */

ol.interaction.Draw.prototype.finishDrawing = function() {

So you can not delete the feature in the drawend event. The above code snippet is self explanatory.

Upvotes: 2

Related Questions