Search code examples
leafletleaflet.draw

Leaflet click to edit layer


i have polyline featuregroup on map and i want to edit layers on click.

if (layer instanceof L.Polyline) {
            const style = {
                color: InvestmentConstants.colors[$ctrl.investment.sector]
            };
            layer.setStyle(style);
            layer.enableEdit();
            layer.on('click',(layer) =>{
                someCb()
            })
        }

I have this but enableEdit function not work throws enableEdit is not a function error.

And map initialize snippet :

 $ctrl.map = new Map(
                "investment-edit-map",
                projectCenter,
                15,
                { 
                  mapSource: Map.Google,
                  editable:true 
                }
            );

Thanks for any kind of help.

Edit : Changed enableEdit() functions line and now i got new error TypeError: Cannot read properties of undefined (reading 'editTools')


Solution

  • According to this documentation .on('click') returns a MouseEvent so this code looks wrong

     layer.on('click',(layer) => {
         // layer here is a MouseEvent, not a layer
         layer.enableEdit(); // MouseEvent.enableEdit() is not a function
     })
    

    Change it to

     layer.on('click',() => {
         layer.enableEdit();
     })
    

    EDIT: The question was edited after my answer which makes my answer deprecated