我基本上有一个状态数组,我遍历它们,为每个状态创建一个标记,然后创建一个信息框,当悬停在每个标记上时应该出现(并在鼠标移出时消失)。我的麻烦是,当标记正确显示时,所有状态标记的信息框都显示最后的信息框内容。这可能与变量作用域和/或异步执行有关 - 我该如何解决?
(当我使用像 Firebug 这样的调试器逐步完成它时,它似乎正在为每个状态正确构建文本 - 但不知何故,它在最后显示了相同的信息)
循环中的适用代码(有些简化):
mark = new google.maps.Marker({
map: map,
position: center,
icon:markerImage,
stateIndex: i // custom attribute for state index so that we can access allStates[index] from within the marker event callbacks
});
stateMarkers.push(mark);
// text for info box
var info = statesArray[i].name + "<br/>" + "Total: " + statesArray[i].total + "<br/>";
//set text for infoBox
var boxText = '<div class="stateMarker">' + info + '</div>';
//set options for infoBox
var myOptions = {
content: boxText,
disableAutoPan: true,
maxWidth: 0,
zIndex: null,
boxStyle: {
background: "url('tipbox.gif') no-repeat",
opacity: 1,
width: "75px"
},
closeBoxMargin: "10px 2px 2px 2px",
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false
};
//instantiate infoBox with options set in myOptions
var ib = new InfoBox(myOptions);
//create mouseover listener for marker on each state
google.maps.event.addListener(mark, 'mouseover', function () {
ib.open(map, this); //open infoBox
});
//create mouseout listener for marker on each state
google.maps.event.addListener(mark, 'mouseout',function(){
ib.close(map,this); //close infoBox
});