使用基本示例无法在地图上添加元素 在drawend事件之后它消失 在drawend监听器元素中存在 使用默认示例页面中的代码给出相同的结果
private map: OlMap;
private drawSource: VectorSource = new VectorSource({
wrapX: false
});
private drawLayer: VectorLayer<VectorSource> = new VectorLayer({
source: this.drawSource
});
private raster = new TileLayer({
source: new OSM()
});
private drawAction: Draw;
private snap: Snap;
constructor() {
}
ngOnInit(): void {
this.initMap();
this.startDraw();
}
private initMap(): void {
const mapCentre: number[] = fromLonLat(this.defaultMapCenter);
this.map = new OlMap({
layers: [
this.drawLayer,
this.raster
],
target: 'map',
view: new View({
center: mapCentre,
zoom: 8, maxZoom: 22,
}),
controls: [],
});
this.map.addLayer(basemapLayer);
})
}
private startDraw() {
this.drawAction = new Draw({
source: this.drawSource,
type: 'Polygon'
});
this.map.addInteraction(this.drawAction);
this.snap = new Snap({source: this.drawSource});
this.map.addInteraction(this.snap)
}) }
更改图层顺序,以便绘制的要素不会隐藏在栅格下方
layers: [
this.raster,
this.drawLayer
],