2

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

Brett
  • 1,717
  • 1
  • 26
  • 38
  • 2
    Do you only need to add a property to the layer or do you need the geojson properties? If you need a normal property you can add in the options what ever you want with: `map.pm.Draw.setPathOptions({custom:"prop"});` and read it out with `layer.options.custom`. If you need the structure from geojson, you have to make it like you do. A simple example: https://jsfiddle.net/falkedesign/og3eka04/ look in the console. – Falke Design Mar 30 '20 at 10:08
  • 1
    Thank you for your comment, I need the geojson props aswell. The creation process needs to add custom props and save it remote so that I can re-load and edit and upload etc... – Brett Mar 30 '20 at 21:35

0 Answers0