Openlayers 3 remove drawn feature

Viewed 3709

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!!

3 Answers

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);
  });
Related