OpenLayer 中标记事件的奇怪行为
我的地图上有标记图层。
每次添加新标记时,我都会将其注册到鼠标单击事件:
var lonlat = new OpenLayers.LonLat(lon,lat);
var marker = new OpenLayers.Marker(lonlat,icon);
marker.id = callId;
marker.events.register("mousedown", marker, function() {AddPopup(marker.id);});
callMarkers.addMarker(marker);
有时我想禁用/启用该事件。所以我使用这些函数:
function EnableAllMarkers()
{
for (var i in callMarkers.markers)
{
callMarkers.markers[i].events.remove("mousedown");
callMarkers.markers[i].events.register("mousedown", callMarkers.markers[i],
function() { AddPopup(callMarkers.markers[i].id); });
}
}
function DisableAllMarkers()
{
for (var i in callMarkers.markers)
{
callMarkers.markers[i].events.remove("mousedown");
}
}
当我使用此代码时,我会得到奇怪的行为 - 有时会打开一个弹出窗口,显示错误的标记。
我单击标记 X,然后弹出窗口 Y 打开。
有人可以帮我吗?
注:EnableAllmMarkers
首先删除该事件的原因是我们不知道添加新标记后是否曾经调用过 DisableAllmMarkers
。如果确实被调用,remove 函数将不会执行任何操作。
I have markers layer on my map.
Every time I add a new marker I register it to a mouse-click event:
var lonlat = new OpenLayers.LonLat(lon,lat);
var marker = new OpenLayers.Marker(lonlat,icon);
marker.id = callId;
marker.events.register("mousedown", marker, function() {AddPopup(marker.id);});
callMarkers.addMarker(marker);
Sometimes I want to disable/enable the event. so I use these functions:
function EnableAllMarkers()
{
for (var i in callMarkers.markers)
{
callMarkers.markers[i].events.remove("mousedown");
callMarkers.markers[i].events.register("mousedown", callMarkers.markers[i],
function() { AddPopup(callMarkers.markers[i].id); });
}
}
function DisableAllMarkers()
{
for (var i in callMarkers.markers)
{
callMarkers.markers[i].events.remove("mousedown");
}
}
When I use this code I get strange behavior - sometimes a popup opens for the wrong marker.
I click on marker X and popup Y opens.
Can someone help me, please?
Note:
The reason EnableAllmMarkers
first removes the event is because we don't know if DisableAllmMarkers
was ever called since a new marker was added. if it was called indeed, remove function will do nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个经典的 JavaScript 陷阱:您在循环中将函数实例化为事件处理程序,并且这些函数引用局部变量。问题是它们全部都引用相同局部变量:相同、单一、唯一、内存中只有一个位置的变量。本例中的变量是“i”。
在 for 循环结束时,“i”将具有对象中最后一个键的值(顺便说一下,如果 callMarkers.markers 确实是一个数组,那么这不应该是一个
for ... in
循环,但这是一个单独的问题)。因此,当这些事件最终触发时,所有处理程序都将使用等于同一个键的“i”来执行操作。修复:
这引入了一个中间匿名函数。该函数立即被调用,并传递“i”的当前值。通过这样做——将“i”作为参数传递给匿名函数——该值被“捕获”在参数“ii”中。每次循环迭代都会导致匿名函数的另一次调用,并且它返回的函数(实际处理程序)将有权访问其自己的私有“ii”变量。
还有一些其他方法可以实现相同的目标,但它们都只是这个主题的变体。
This is a classic JavaScript trap: you're instantiating functions as event handlers in a loop, and the functions refer to a local variable. The problem is that all of them refer to the same local variable: the same, single, unique, only-one-place-in-memory variable. The variable in this case is "i".
At the end of that
for
loop, "i" will have the value of the last key in the object (and, by the way, ifcallMarkers.markers
is really an array, then this shouldn't be afor ... in
loop anyway, but that's a separate issue). When those events finally fire, therefore, all the handlers will do their thing with "i" equal to that one same key.To fix:
That introduces an intermediary anonymous function. That function is immediately called, and passed the current value of "i". By doing that — passing "i" as an argument to the anonymous function — the value is "captured" in the argument "ii". Each loop iteration will cause another invocation of the anonymous function, and the function it returns (the actual handler) will have access to its own private "ii" variable.
There are some other ways to achieve the same thing, but they're all just variations on this theme.
我遇到了同样的问题,一旦事件注册到特定的制造商,它也会触发所有其他标记。最后,我能够解决它。我必须为每个标记注册单独的事件。以下是我的代码:
I had the same issue, once the event is registered to a specific Maker it triggers for all the other Marker's as well. Finally, I was able to solve it. I had to register seperate events for each marker. Following is my code: