Maximilien Faure
Maximilien Faure

Reputation: 119

How can I delete a feature after a callback on openlayers

I try to create a code who can create a feature and delete it if it dosen't respect some rules. (in this exemple I just delete it just after his creation)

Below is my code, I create a map and I allow to create a polygon on. Next I try to delete it when an event drawend is triggered.

 init(){
        this.message = "";
        var raster = new TileLayer({
            source: new XYZ({
                url: 'http://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',
                attributions: [
                '© Google','<a href="https://developers.google.com/maps/terms">Terms of Use.</a>'
                ]
            })
        });

        this.source = new VectorSource();

        this.vector = new VectorLayer({
            source: this.source
        });

        this.map = new Map({
            layers: [raster, this.vector],
            target: 'map',
            view : new View({
                center: [0,0],
                zoom : 4
            })
        }); 

            this.addInteraction();


    }

    addInteraction() {
        var self = this;
        this.draw = new Draw({
            source: this.source,
            type: "Polygon"
        });
        this.draw.on('drawend', function (e) {
            self.message = "";
            self.map.removeInteraction(self.draw);
            self.selectedFeature = e.feature;

            self.removeSelectedFeature();

            // This works but I don't like this solution
            //setTimeout(function(){self.removeSelectedFeature()}, 1);

            self.geoJSON = self.getCoord();
        });
        this.map.addInteraction(this.draw);
    }

    removeSelectedFeature()
    {
        this.source.removeFeature(this.selectedFeature);
        this.selectedFeature = null;
        this.validated = false;
        this.addInteraction();
    }

but When I try to execute this code, I got this error :

core.js:14597 ERROR TypeError: Cannot read property 'forEach' of undefined
    at VectorSource.removeFeatureInternal (Vector.js:951)
    at VectorSource.removeFeature (Vector.js:939)
    at MapComponent.push../src/app/map/map.component.ts.MapComponent.removeSelectedFeature (map.component.ts:106)
    at Draw.<anonymous> (map.component.ts:97)
    at Draw.boundListener (events.js:41)
    at Draw.dispatchEvent (Target.js:99)
    at Draw.finishDrawing (Draw.js:872)
    at Draw.handleUpEvent (Draw.js:578)
    at Draw.handleEvent (Pointer.js:132)
    at Draw.handleEvent (Draw.js:524)

I know it's because my feature isn't exist yet, and it will created after the event will finish. hence my question, how can I delete a feature after it was created ?

  1. I tried using a event button but it's not a good idea I would like that the user don't need to click on a button for execute checks.
  2. I tried using a setTimeout (as we can see in my commented code) but I don't like this solution (even if it works)

Anyone can explain me how can I do the job ?

Thanks in advance ;)

Upvotes: 0

Views: 506

Answers (1)

Rob
Rob

Reputation: 659

have you tried something like this in your drawend, if your check failed:

source.once('addfeature', (event) => {
  const feature = event.feature;
  source.removeFeature(feature);
});

this listens to the 'addfeature' event once and then removes the listener.

Upvotes: 3

Related Questions