简体   繁体   中英

How do I limit the number of polygons in a layer?

My code currently allows the user to have the ability to create many polygons at once. I would like it to instead erase the previous vector everytime the user decides to create a new one.

let drawSource = new VectorSource({wrapX: false}); 

let drawLayer = new VectorLayer({source : drawSource,})
this.map.addLayer(drawLayer);

let draw = new Draw({
  source : drawSource,
  type : GeometryType.POLYGON,
  style : new Style({
    stroke: new Stroke({
          color: "#f00",
          width: 1,
        }),
    fill: new Fill({
      color: "#300",
    })
  })
});
this.map.addInteraction(draw)

Clear the source when a new drawing is started

draw.on('drawstart', function() {
  drawSource.clear();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM