我基本上有一个状态数组,我通过它们循环,为每个状态创建一个标记,然后创建一个信息框,当它悬停在每个标记上(并在mouseout上消失)时应该出现。我的麻烦是,虽然标记正常显示,但所有状态标记的信息框都显示最后一个信息框内容。这可能需要做一些变量范围和/或异步执行 - 我该如何解决它?Google Maps API:循环中的信息框 - 为什么所有信息框都显示最后一个值?
(当我通过它一步有类似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
});
可能重复(http://stackoverflow.com/questions/4897316/google-maps-api-v3-infowindow-all-infowindows-displaying - 同名内容) – geocodezip
[Google Maps Api v3:信息窗口显示地图上所有标记的相同信息]的可能重复(http://stackoverflow.com/questions/4236522/google-maps-api-v3- info-window-displays-same-information-for-all-the-markers) – geocodezip
[Google Maps API多个标记与Infowindows]的可能重复(http://stackoverflow.com/questions/11106671/google-maps-api-多标记与infowindows) – geocodezip