Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a google map marker drag operation?

As you see in the following code, the Markers are draggable. In some cases, If a marker is dropped in a wrong area(polygon) I would like to cancel the drop event and consequently the marker come back to its original location automatically.

function placeMarker(latlng, color, isDraggable) {
    var marker = new GMarker(latlng, { icon: getIcon(color), draggable: true });
    map.addOverlay(marker);
    GEvent.addListener(marker, "dragend", function () {
        //redraw polygons again
    });
    GEvent.addListener(marker, "click", function () {
        var latlng = marker.getPoint();
        map.openInfoWindowHtml(marker.getPoint(), latlng.y + ", " + latlng.x);
    });
    return marker;
}

The question is how can I cancel the drag event of marker when it's already dropped in dragend event?

like image 582
Daniel B Avatar asked Feb 26 '11 23:02

Daniel B


1 Answers

Still couldn't find any solutions after these years, and yet again encountering this issue. I wish there was event.cancelDrag()to call.

All I can think of is to check if the marker belongs to mypolygon if not move it back to where it was before:

  • On dragstart save the current location as preLocation
  • on dragend use containsLocation(e.latLng, mypolygon)
    • yes => do nothing
    • no: set the marker location to preLocation.

google map containsLocation

google map Events

like image 52
Daniel B Avatar answered Oct 20 '22 20:10

Daniel B