You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenLayers绘制图形后drawend事件触发图形消失问题求助

OpenLayers 绘制图形后自动消失问题

问题说明

使用基础示例无法在地图上添加元素,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);
}

效果截图

  • 绘制前状态:绘制前
  • 绘制后状态:绘制后

内容的提问来源于stack exchange,提问作者Maksym

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:40:54