简体   繁体   中英

Remove or Delete feature in vector.layer

Now, I had problem on removing entities that is already displayed on openlayers. I have two sources KML that contains the path of a vehicle.

kml1 10 placemarks kml2 50 placemarks

I first displays the kml1 that stores in start.vectorsource variable the loading the page and after using a tool that needs to display the kml2 and stores in the start.vectorfiltered variable. Comparing the two variables content so if there is an element ( feature ) of start.vectorfiltered in start.vectorsource , it removes this item Here is an excerpt of the code:

start.vectorsource.getSource().forEachFeature(function (feature) {
            if (typeof feature.getId() !== "undefined") {
                if (feature.getId().indexOf(mobileName) !== -1) {
                    entity.push(feature.getId());
                }
            }

        });
        start.vectorfiltered.getSource().getFeatures().forEach(function (feature) {
            if (typeof feature.getId() !== "undefined") {
                for (var k = 0; k < entity.length; k++) {
                    if (feature.getId() === entity[k]) {
                        console.log(feature.getId() + " " + entity[k]);
                        start.vectorsource.getSource().removeFeature(feature);
                    }
                }
            }
            interfaces.newFeature.push(feature);
        });

I analyze a start.vectorsource to retrieve the item ID (feature.getId ()) and I'm looking on the ID if it contains the vehicle name (mobilename). if it has, I store the ID in the variable entity

I analyze a start.vectorfiltered and I compare with the variable entity, if they are equal, it removes the feature in start.vectorsource by its ID.

Here is the error gets :

TypeError: this.s[b] is undefined http://localhost:7299/js/ol3/ol.js Line 636

I think the problem comes from the fact that you don't have an actual ID so feature.getId() won't work.

Try instead feature.get('id') this is probably due to the fact that your ID is only an attribute of the feature

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM