捕捉到最近的标记 [英] Snap to nearest marker

查看:122
本文介绍了捕捉到最近的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GoogleMaps,并且我有2个或更多标记,并且可拖动。
如果他们靠近并将它们合并为1,我想捕捉2个标记。
是可能的吗?

有人可以给我指针吗?我怎么才能意识到这一点?

解决方案

您需要处理拖放活动上的 GMarker 对象。诀窍是当你发现自己足够接近另一个标记以将它们拼合在一起时,你会做什么。我围绕这一点玩了一番,并认为可能会隐藏当前拖动的标记可能

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

//遍历你的点和每个其他点...
if(near(point,otherPoint))
{
//隐藏这个标记
标记。 hide();

//移动附近标记以表示合并?

//然后删除dragend上拖动的标记(如果合并)
}
}

不是完全优雅的解决方案,但它可能适合您的目的。



编辑:我想知道您是否在查找代码来检查附近的点,所以我更新了示例以执行此操作:

 函数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;

返回false;
}


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 ?

解决方案

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

这篇关于捕捉到最近的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆