Question

The following code receives an error on the lines for enabling and disabling the marker dragging ("Unable to get property 'disable' of undefined or null reference"). The markers show up on the map just fine and are draggable as the creation line indicates. Placing an alert in place of the enable line produces a proper object so I believe the marker is defined. Is there something I need to do to enable the IHandler interface? Or am I missing something else?

var marker = L.marker(L.latLng(lat,lon), {icon:myIcon, draggable:'true'})
    .bindLabel(name, {noHide: true,direction: 'right'});
marker._myId = name;
if (mode === 0) {
    marker.dragging.enable();
} else {
    marker.dragging.disable();
}
Was it helpful?

Solution 3

I haven't found an answer but my workaround was this:

var temp;
if (mode === 0) {
    temp = true;
} else {
    temp = false;
}
var marker = L.marker(L.latLng(lat,lon), {icon:myIcon, draggable:temp})
    .bindLabel(name, {noHide: true,direction: 'right'});
marker._myId = name;

Fortunately I change my icon when it is draggable.

OTHER TIPS

I had a similar problem today (perhaps the same one) it was due to a bug in leaflet (see leaflet issue #2578) where changing the icon of a marker invalidates any drag handling set on that marker. This makes any calls to marker.dragging.disable() fail.

The fix hasn't made it into leaflets master at time of writing. A workaround is to change the icon after updating the draggable status if possible.

marker.dragging.disable();
marker.setIcon(marker_icon);

Use the following code to make an object draggable. Set elementToDrag to the object you wish to make draggable, which is in your case: "marker"

var draggable = new L.Draggable(elementToDrag);
draggable.enable(); 

To disable dragging, use the following code:

draggable.disable()

A class for making DOM elements draggable (including touch support). Used internally for map and marker dragging. Only works for elements that were positioned with DomUtil#setPosition

leaflet: Draggable

If you wish to only disable the drag option of a marker, then you can use the following code (where "marker" is the name of your marker object):

marker.dragging.disable(); 
marker.dragging.enable();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top