Search code examples
javascriptopenlayers

OpenLayer draw remove shape after drawend


Using basic example cant add element on map After drawend event its disapear in drawend listener element is present Using code from default example page gives same result

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)

}) }

Before drawend Before drawend

After After drawend


Solution

  • Change the layer order so the drawn features are not hidden beneath the raster

      layers: [
        this.raster,
        this.drawLayer
      ],