I would like to know if it's possible to update the draggable event of a marker inside a GEOJSON layer in leaflet, I know I can do this by adding:
layer.options.draggable = True
Inside the onEachFeature function, what I'm trying to achieve is, to update the draggable options on an element click, something like:
$(document).on('click','#someButton',function(){
layer.options.draggable = True; //Only one specific marker
});
This way I would like to have all my marker with draggable options disabled, then on a button click, enable the draggable option, only for one specific marker. Is it possible to achieve this using geojson layer? I also have this geojson layer inside a featureGroup, hope you guys can help me out. Thanks
You can dynamically enable / disable the draggability of a Leaflet Marker by simply removing it from the map, setting (resetting) its
draggable
option, then re-adding it to the map:Whether you create your Marker through a GeoJSON Leaflet layer group or not is irrelevant.
Every
L.Marker
has adragging
property, which is the marker'sHandler
for dragging - and Leaflet handlers can beenable()
d anddisable()
d.The Leaflet documentation about
L.Marker
'sdragging
property also explicitly states:In your specific case, this would mean something like:
Be aware that this only works for
L.Marker
s - if you are using GeoJSON, do not expect this to work on lines or polygons, or on points that you specifically decided to display asL.Circle
s orL.CircleMarker
s (via thepointToLayer
option ofL.GeoJSON
)