The problem is that when I try and remove the feature the this.vectorLayer.getSource().removeFeature()
is throwing an error Cannot read property 'forEach' of undefined
I can logged the feature absolutely fine so it's definitely grabbing it in the remove function, I also tried to add it to an array in a features property on the vectorLayer but couldn't seem to get that to work or find any decent documentation on it.
I should note that I've set up buttons to independently add the layer at the moment so I will add the layer with the addLayer and then draw a shape with addInteraction then try and remove said drawing.
/** 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);
}
答案 0 :(得分:2)
以此替换您的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;
});
});
}