简体   繁体   English

OpenLayer 中标记事件的奇怪行为

[英]Strange behavior of marker events in OpenLayer

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.我单击标记 X 并打开弹出窗口 Y。

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. EnableAllmMarkers首先删除该事件的原因是因为我们不知道自从添加了新标记后DisableAllmMarkers是否被调用过。 if it was called indeed, remove function will do nothing.如果确实被调用,remove 函数将不执行任何操作。

This is a classic JavaScript trap: you're instantiating functions as event handlers in a loop, and the functions refer to a local variable.这是一个典型的 JavaScript 陷阱:您将函数实例化为循环中的事件处理程序,并且这些函数引用一个局部变量。 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".这种情况下的变量是“i”。

At the end of that for loop, "i" will have the value of the last key in the object (and, by the way, if callMarkers.markers is really an array, then this shouldn't be a for ... in loop anyway, but that's a separate issue).for循环结束时,“i”将拥有对象中最后一个键的值(顺便说一句,如果callMarkers.markers确实是一个数组,那么这不应该是一个for ... in无论如何循环,但这是一个单独的问题)。 When those events finally fire, therefore, all the handlers will do their thing with "i" equal to that one same key.因此,当这些事件最终触发时,所有处理程序都将使用等于同一个键的“i”来执行它们的操作。

To fix:修复:

  for (var i in callMarkers.markers)
    {
        callMarkers.markers[i].events.remove("mousedown");               

        callMarkers.markers[i].events.register(
          "mousedown", 
          callMarkers.markers[i],
          (function(ii) {
            return function() {
              AddPopup(callMarkers.markers[ii].id);
            }
          )(i)
         );
    } 

That introduces an intermediary anonymous function.这引入了一个中间匿名函数。 That function is immediately called, and passed the current value of "i".该函数立即被调用,并传递“i”的当前值。 By doing that — passing "i" as an argument to the anonymous function — the value is "captured" in the argument "ii".通过这样做——将“i”作为参数传递给匿名函数——值在参数“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.每次循环迭代都会导致匿名函数的另一次调用,并且它返回的函数(实际处理程序)将有权访问其自己的私有“ii”变量。

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.我遇到了同样的问题,一旦事件注册到特定的 Maker,它也会为所有其他 Marker 触发。 Finally, I was able to solve it.最后,我能够解决它。 I had to register seperate events for each marker.我必须为每个标记注册单独的事件。 Following is my code:以下是我的代码:

    var makerCount=0; // I want only 2 Markers to be shown : Source,Destination

function setMarkers(x,y){
    var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);  

 if(makerCount<2){

    if(makerCount==0){  // Source
       var location = new OpenLayers.LonLat(x,y); 

       var size = new OpenLayers.Size(21,25);
       var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);

        var sourceMarker=new OpenLayers.Marker(location,icon)
        sourceMarker.events.register('mousedown', sourceMarker, function(evt) { 
                alert('Source :: X='+ x + ' , Y=' + y); 
                OpenLayers.Event.stop(evt); }); 


        markers.addMarker(sourceMarker); 
        markers.setOpacity(0.2);
        makerCount++;
    }else{ // Destination

        var location = new OpenLayers.LonLat(x,y); 

        var size = new OpenLayers.Size(21,25);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var halfIcon = icon.clone();  

        var destinationMarker=new OpenLayers.Marker(location,halfIcon)
        destinationMarker.events.register('mousedown', destinationMarker, function(evt) { 
                alert('Destination :: X='+ x + ' , Y=' + y); 
                OpenLayers.Event.stop(evt); 
           });          
        markers.addMarker(destinationMarker); 
        halfIcon.setOpacity(0.5);
        makerCount++;
    }

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM