简体   繁体   English

如何为Bing Maps事件创建关闭?

[英]How to create a closure for a Bing Maps event?

I have a javascript problem that I believe is related to closures in loops similar to: 我有一个JavaScript问题,我认为这与循环中的闭包有关,类似于:

How to copy a variable in JavaScript? 如何在JavaScript中复制变量? JavaScript closure inside loops – simple practical example 循环内的JavaScript闭合–简单的实际示例

I'm plotting a list of pins on a Bing map and attaching an event to each pin so that an infobox can be displayed for each pin. 我正在Bing地图上绘制引脚列表,并将事件附加到每个引脚,以便可以为每个引脚显示一个信息框。 Each pin is supposed to have a different info box. 每个引脚应具有不同的信息框。 The problem is that when the event fires it always displays the very last info box from the last iteration of the loop, not the info box that goes with the pin firing the event. 问题在于,事件触发时,它始终显示循环的最后一次迭代中的最后一个信息框,而不是触发事件的引脚附带的信息框。 Here is the method with the looping construct and my failed attempt to create a closure 这是带有循环构造的方法,但我尝试创建闭合的尝试失败

monumentMap.plotData = function() {
        monumentMap.theMap.entities.clear();

        var monument = null
        var loc = null;
        for (var i = monumentMap.theData.length - 1; i >= 0; i--) {
            monument = monumentMap.theData[i];
            loc = new Microsoft.Maps.Location(monument.lat, monument.lon);

            // construct a bing pushpin and add it to the item data
            monument.pin = new Microsoft.Maps.Pushpin(loc, {
                icon: 'http://media.maps101.com/' + monument.pinImg
            });

            // construct a bing infobox and add it to the item data
            monument.infobox = new Microsoft.Maps.Infobox(loc, { // breakpoint 1
                title: monument.title,
                offset: new Microsoft.Maps.Point(-3, monument.pin.getHeight() - 5),
                zIndex: 999,
                visible: true
            });

            // add event listeners
            // doesn't work
            Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover', function() {
                return function(infobox) {
                    monumentMap.displayInfobox(infobox);
                }(monument.infobox); // breakpoint 2: by the time this is executed monument.infobox is already "last not current"
            });

            // add the entities to the map view for this item
            monumentMap.theMap.entities.push(monument.pin);
        }
}

I'm having trouble getting my head around why it doesn't work. 我很难弄清为什么它不起作用。 I set monument.infobox right there in the loop and if I put a breakpoint on the line marked "breakpoint 1" I can see that monument.infobox changes on every loop iteration, yet when I get to "breakpoint 2" (which doesn't get executed until the event fires) it references the last infobox i created, not the infobox that I attached to that pin when attaching the event handler. 我在循环中在那里设置了Monument.infobox,如果在标记为“ breakpoint 1”的行上放置一个断点,我可以看到Monument.infobox在每次循环迭代中都会改变,但是当我到达“ breakpoint 2”时(它不会在事件触发之前不会执行),它引用我创建的最后一个信息框,而不是附加事件处理程序时附加到该引脚的信息框。

By this point I half-expect the line monumentMap.theMap.entities.push(monument.pin); 到此为止,我半期待了MonumentMap.theMap.entities.push(monument.pin); to add a bunch of copies of the last pin in the loop, but it doesn't. 在循环中添加一堆最后一个引脚的副本,但事实并非如此。 It works just as it should and adds a different pin each loop iteration. 它按其应有的方式工作,并在每次循环迭代中添加一个不同的引脚。

You need to put the parameter in the outer anominous function. 您需要将参数放入外部阳极功能中。 If I'm correct: 如果我是正确的:

Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover', 
(function(infobox) {
  return function() {
    monumentMap.displayInfobox(infobox);
  }     
 })(monument.infobox));

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

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