Snap to nearest marker

I am using GoogleMaps and I have 2 or more markers and they are dragging. I want to snap 2 markers if they are near and combine them into 1. is it possible?

Can someone give me pointers .. how can I figure this out?

+2


source to share


1 answer


You need to handle the drop event on the GMarker . The trick is what you do when you find that you are close enough to another marker to tie them together. I played around a bit with this and thought that maybe hiding the current 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 exactly a neat solution, but it might suit your goals.



Edit . I was wondering if you were looking for code to check for nearby points, so I updated my example to do this:

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;
}

      

+2


source







All Articles