如何限制图层中的多边形数量?

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

我的代码目前允许用户一次创建多个多边形。我希望它在每次用户决定创建一个新矢量时擦除以前的矢量。

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)

开始新绘图时清除源

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