简体   繁体   English

删除或删除vector.layer中的功能

[英]Remove or Delete feature in vector.layer

Now, I had problem on removing entities that is already displayed on openlayers. 现在,我在删除已经显示在openlayers上的实体时遇到问题。 I have two sources KML that contains the path of a vehicle. 我有两个来源KML,其中包含车辆的行驶路线。

kml1 10 placemarks kml2 50 placemarks kml1 10地标kml2 50地标

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. 首先,使用加载需要显示kml2并将其存储在start.vectorfiltered变量中的工具后,加载页面时,首先显示存储在start.vectorsource变量中的kml1。 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.vectorfiltered在start.vectorsource元素( 功能 )相比,它消除了这个项目下面是代码的摘录:

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). 我分析了一个start.vectorsource来检索商品ID(feature.getId()),如果它包含车辆名称(mobilename),我正在寻找该ID。 if it has, I store the ID in the variable entity 如果有,我将ID存储在变量实体中

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. 我分析了一个start.vectorfiltered,并与变量实体进行比较,如果它们相等,它将通过其ID删除start.vectorsource中的功能。

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. 我认为问题出在您没有实际的ID,因此feature.getId()无法使用。

Try instead feature.get('id') this is probably due to the fact that your ID is only an attribute of the feature 尝试改用feature.get('id')这可能是由于您的ID仅是功能的一个属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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