Snap to nearest marker

不想你离开。 提交于 2020-01-21 05:42:08

问题


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:


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


来源:https://stackoverflow.com/questions/1484578/snap-to-nearest-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!