Leaflet 绘制事件 "draw:deleted" 未删除图层

Leaflet draw event "draw:deleted" not deleting layer

请注意,我为 Angular 使用了@asymmetrik/ngx-leaflet-draw。 我正在配置一个地图,用户可以在其中绘制矩形或多边形。然后我将这些保存到基于 onDrawCreated 事件的 layerGroup,这工作得很好。

我还打算根据 onDrawDeleted 事件删除图层,但由于某些原因这不起作用。我最终在应用程序中做的是保存数据并将其转换为 GeoJSON。然后在控制台中显示它以进行故障排除。

组件HTML:

<button (click)="checked = !checked">Edit Mode</button>
<button (click)="onSave()">Save</button>
<div id="image-map" 
leaflet 
[leafletOptions]="options" 
[leafletLayersControl]="layersControl"
    (leafletMapReady)="onMapReady($event)">
    <div *ngIf="checked" 
    leafletDraw 
    [leafletDrawOptions]="drawOptions" 
    (leafletDrawCreated)="onDrawCreated($event)" 
    (leafletDrawDeleted)="onDrawDeleted($event)">
    </div>
</div>

部分地图和传单绘制组件:

  zones = L.featureGroup();

  onDrawCreated(e: any) {
    this.zones.addLayer(e.layer);
    console.log('Draw Created Event!', e);
    console.log(this.zones.getLayers());
  }

  onDrawDeleted(e: any) {
    this.zones.removeLayer(e);
    console.log('Draw Deleted Event!', e);
    console.log(this.zones.getLayers());
  }

  onSave() {
    const data = this.zones.toGeoJSON();
    console.log(data);
  }

根据文档和其他类似问题,上面的代码应该可以工作。但我想我错过了一些简单的东西。

遍历已删除的图层以删除已删除的图层。如果只有一个,那么它将被删除。

onDrawDeleted(e: any) {
    e.layers.eachLayer(layer => {
      this.zones.removeLayer(layer);
    });
    console.log("Draw Deleted Event!", e);
    console.log(this.zones.getLayers());
  }

Demo