javascriptleafletcartographyleaflet-geoman

Is there a way to disable a marker or a polygon to be draggable in leaflet-geoman?


As the title says, I have a set of different markers and polygons on a map. All I want to do is to disable the markers and the polygons I create on the map to be dragged and only make circles draggable. As far as I read the documentation, there's no way to do that in drag mode.


Solution

  • You are right, there is no way to disable the drag.

    But you can use this workaround:

    function enableDrag(){
        map.eachLayer((layer)=>{
            if(layer._dragDisabled){
                layer._pmTempLayer = false;
                layer._dragDisabled = false;
            }
        });
    }
    
    function disableDrag(){
        map.eachLayer((layer)=>{
            if(layer instanceof L.Circle){
                layer._pmTempLayer = true;
                layer._dragDisabled = true;
            }
        });
    }
    

    When a layer has the property _pmTempLayer it is filtered out in the drag function.