views:

194

answers:

1

Hi there ,

i am using GoogleMaps and i have 2 or more markers and they are draggable. I want to snap 2 markers if they are near and merge them into 1. is this possible ?

Can someone give me pointers .. how i can realize that ?

+1  A: 

You need to handle the drag event on the GMarker object. The trick is what do you do when you detect that you are near enough to another marker to snap them together. I played around a little with this and thought maybe hiding the currently dragged marker might be a good way to go.

GEvent.addListener(marker, "drag", function(point) {

    // iterate over your points and for each otherPoint...
    if (near (point, otherPoint))
    {
        // hide this marker
        marker.hide ();

        // move nearby marker to indicate merge?

        // then delete the dragged marker on the dragend (if it was merged)
    }
}

Not an entirely elegant solution, but it might suit your purposes.

Edit: I wondered if you were looking for the code to check nearby points, so I updated my example to do that:

function near (point1, point2)
{
    sw = new GLatLng(point2.lat() - 0.005, point2.lng() - 0.005);
    ne = new GLatLng(point2.lat() + 0.005, point2.lng() + 0.005);
    var bounds = new GLatLngBounds(sw, ne);
    if (bounds.contains (point1))
     return true;

    return false;
}
Cannonade