slutske22 / leaflet-arrowheads

A small plugin for leaflet to quickly draw arrowheads on polylines for vector visualization
https://codesandbox.io/s/leaflet-arrowheads-example-zfxxc
MIT License
164 stars 29 forks source link

[Question] compatibility with leaflet geoman? #44

Closed AndrejGajdos closed 1 year ago

AndrejGajdos commented 1 year ago

I am trying to use this library with leaflet-geoman. Leaflet geoman has an option allowEditing: false to disable editing for selected layers. It works in general, but for some reason it doesn't work for layers generated by arrowheads. I am not sure if this is good place to ask this question.

jsfiddle

L.PM.Utils.findLayers(map).forEach((layer) => {
   if (layer?._arrowheads){
    const arrows = layer.getArrowheads()
        console.log({arrows})
    arrows?.pm.setOptions({ allowEditing: false })
        arrows?.eachLayer(arrowLayer => {
           console.log({arrowLayer})
           arrowLayer?.pm.setOptions({ allowEditing: false })
        })
  }
})
Screenshot 2023-09-24 at 10 13 51
slutske22 commented 1 year ago

This was quite tricky. The issue is that the arrowheads need to be updated every time the map moves, and we lose the custom options you had set with .pm.setOptions every time. So you have to readd them:

/**
 * Sets options on arrowheads after they were added to map
 */
const setPmOptions = (geoJsonLayer) => geoJsonLayer.eachLayer(layer => {
  // The issue is that this is a new instance of L.layerGroup on every map move - maybe should be fixed in src code
  const arrows = layer.getArrowheads();
  arrows.eachLayer(l => {
    l.pm.setOptions({
      allowEditing: false
    })
  })

})

const parsed = routes.map(route => {
  const geojson = JSON.parse(route.geojsonString);
  const geoJsonLayer = L.geoJSON(geojson, {
    arrowheads: {
      size: '25%',
      frequency: 'endonly',
    },
  })
  geoJsonLayer.addTo(map)
  setPmOptions(geoJsonLayer);
  return geoJsonLayer;
})

map.on('moveend', () => {
  parsed.forEach(geoJsonLayer => {
    setPmOptions(geoJsonLayer)
  })
})

jsfiddle

AndrejGajdos commented 1 year ago

@slutske22 thanks!