Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Openlayers 4.6.5和;Angular 6无法删除像素处绘制的形状_Angular_Angular6_Openlayers_Openlayers 3_Angular Openlayers - Fatal编程技术网

Openlayers 4.6.5和;Angular 6无法删除像素处绘制的形状

Openlayers 4.6.5和;Angular 6无法删除像素处绘制的形状,angular,angular6,openlayers,openlayers-3,angular-openlayers,Angular,Angular6,Openlayers,Openlayers 3,Angular Openlayers,问题是,当我尝试删除该功能时,this.vectorLayer.getSource().removeffeature()抛出一个错误无法读取未定义的属性“forEach” 我可以记录的功能绝对好,所以它肯定是抓取它的删除功能,我还试图将它添加到数组中的一个功能属性的矢量层,但似乎无法得到工作或找到任何体面的文档 我应该注意的是,我已经设置了按钮来独立添加图层,所以我将使用addLayer添加图层,然后使用addInteraction绘制一个形状,然后尝试删除所述绘图 /** This i

问题是,当我尝试删除该功能时,
this.vectorLayer.getSource().removeffeature()
抛出一个错误
无法读取未定义的属性“forEach”

我可以记录的功能绝对好,所以它肯定是抓取它的删除功能,我还试图将它添加到数组中的一个功能属性的矢量层,但似乎无法得到工作或找到任何体面的文档

我应该注意的是,我已经设置了按钮来独立添加图层,所以我将使用addLayer添加图层,然后使用addInteraction绘制一个形状,然后尝试删除所述绘图

    /** This is the input property for the base layer of the map the main source */
@Input('coreMap') coreMap: CoreMapComponent
vectorLayer: VectorLayer;
modifyLayer: Modify;
draw: Draw;
vectorSource: VectorSource;
style: Style;
snap: Snap;
style2: Style;



ngOnInit(){
    this.vectorSource = new VectorSource({
        wrapX: false,
    })

    this.style = new Style({
        fill: new Fill({
            color: '#ffcc33',
        }),
        stroke: new Stroke({
            color: '#ffcc33',
            width: 2
        }),

        image: new CircleStyle({
            radius: 7,
            fill: new Fill({
                color: '#ffcc33'
            })
        })
    })

    this.vectorLayer = new VectorLayer({
        source: this.vectorSource,
        style: [this.style]
    });

    this.modifyLayer = new Modify({ source: this.vectorSource })
}

addLayer(){
    this.coreMap.layerManager.addLayer(this.vectorLayer)
}

getLayers(){
    console.log(this.coreMap.map.getLayers().getArray());
}

removeShape(){
    this.coreMap.map.removeInteraction(this.draw);
    this.coreMap.map.removeInteraction(this.snap);
    this.coreMap.map.on('click', (evt: any) => {
        this.coreMap.map.forEachFeatureAtPixel(evt.pixel,
            (feature: Feature, layer) => {
                this.vectorLayer.getSource().removeFeature(feature)
                return true;
            });
    });
}

addInteraction() {
    this.draw = new Draw({
        source: this.vectorSource,
        style: this.style,
        type: 'Point'
    })
    this.coreMap.map.addInteraction(this.draw)
    this.snap = new Snap({ source: this.vectorSource })
    this.coreMap.map.addInteraction(this.snap);
    this.coreMap.map.addInteraction(this.modifyLayer);
}

用此函数替换
removeShape()
函数!我相信它会起作用的

removeShape(){
    var self = this;
    this.coreMap.map.removeInteraction(this.draw);
    this.coreMap.map.removeInteraction(this.snap);
    this.coreMap.map.on('click', (evt: any) => {
        self.coreMap.map.forEachFeatureAtPixel(evt.pixel,
            (feature: Feature, layer) => {
                self.vectorLayer.getSource().removeFeature(feature)
                return true;
            });
    });
}

我通过让交互保持活动状态来重现“forEach”错误,它还解释了传递的空层参数。您的代码添加了modifyLayer交互,但没有将其删除。这与Vugar answer一起解决了此问题非常感谢。这与Mikes关于删除modifyLayer交互的评论一起解决了此问题,感谢你们两位帮助解决了一个完整的问题。如果可能的话,你们还可以首先解释问题是什么?Bc,在“forEachFeatureAtPixel”函数中,这不属于角度组件或服务。它的地图是“这个”。在“return true”行之前写入console.log(this),并检查它