How to add properties to leaflet-geoman layer when using the toolbar

Viewed 1621

I need to add custom props to my created polys. To do so currently when the user select in the toolbar the polygon and create a shape, on the create event I convert it to json remove it from the map add the custom props to the json and reload the newly created layer.

this.map.on('pm:create', e => {
      const id = getUID();
      const leafId = e.layer._leaflet_id;
      const featureGroup = L.featureGroup().addLayer(e.layer);

      this.map.eachLayer(layer => {
        if (layer._leaflet_id === leafId) {
          this.map.removeLayer(layer);
        }
      });

      const data = featureGroup.toGeoJSON();
      data.features[0].properties = {
        id,
        name: `Zone ${id}`
      };

      this.zoneService.add({id, data: JSON.stringify(data)})
        .pipe(
          switchMap((res) => this.zoneService.getAll().pipe(this.addToMap(this.map)))
        ).subscribe();
    });

This is working but I feel I am not doing something right here. Adding removing Adding, there must be a better way. Thanks for any help

0 Answers
Related