我正在尝试在地图上放置多个标记,并且还能够单独切换它们,以便一次只显示一个。之前,我能够完美地加载和切换单个信息框。我试图实现的最终目标是一次只打开一个信息框,并且能够访问各个标记信息框。目前,我有多个标记,但信息框不会随功能切换。
代码:
var myCenter = new google.maps.LatLng(30.43, -84.285);//Tallahasse
var myHouse = new google.maps.LatLng(30.438329, -84.29116599999998);//MyHouse
//Initilalize map to center on Tallahassee
function initialize()
{
var mapProp = {
center:myCenter,
zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var marker = new Array();
var ib = new Array();
for(var i = 0; i < 2; i++){ // START LOOP -----------------------------
//Marker(This creates the marker, nothing further is needed)
marker[i] = new google.maps.Marker({
position:myHouse, //Marker Position
map: map, //Which map this marker is on, defaults to current
title: 'More Info', // This displays text when the cursor hovers over the marker
draggable: false,
icon: 'images/sm1beermug.png'
});
var boxText = document.createElement("div");
boxText.style.cssText = "border: 3px solid #2d2d2d;";
boxText.innerHTML = "<strong><font size=\"3\" color = #222222> Taylor's House </font></strong>";
//font attribute not supported in HTML5, progress to using css in later revisions
var myOptions = {
content: boxText,
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-251, 0),//X axis should be half the size of box width
zIndex: null,
boxStyle: {
background: "url('tipbox.gif') no-repeat",
opacity: .94,
width: "502px"
},
closeBoxMargin: "2px 2px 2px 2px",
closeBoxURL: "images/close.png",
infoBoxClearance: new google.maps.Size(10, 10),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false
};
ib[i] = new InfoBox(myOptions);
ib[i].open(map, marker[i]);
ib[i].hide();
google.maps.event.addListener(marker[i], 'click', function() {
if(ib[i].getVisible()){
ib[i].close();
ib[i].open(map, marker[i]);
ib[i].hide();
}
else{
ib[i].show();
}
});
myHouse = new google.maps.LatLng(30.41329, -84.29316599999998);
}//END LOOP ------------------------------
}
google.maps.event.addDomListener(window, 'load', initialize);