2012-11-23 81 views
-1

可能重複:
Google maps: infowindow is not closing如何在點擊標記時關閉所有打開的信息窗口?

我花了幾個小時試圖讓我的周圍如何做到這一點的頭,但似乎並沒有在任何地方接近的分辨率得到。請參閱我的代碼如下:

function initialize() { 
var latlng = new google.maps.LatLng(50.71608,-1.993761); 
var settings = { 
     zoom: 14, 
     center: latlng, 
     mapTypeControl: true, 
     mapTypeControlOptions: {style: 
     google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
     navigationControl: true, 
     navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
var map = new google.maps.Map(document.getElementById("map_canvas"),  settings); 

var contentString = '<div>'+ 
       '<p><strong>name</strong></p>'+ 
       '</div>'; 
var infowindow = new google.maps.InfoWindow({ 
       content: contentString 
      }); 
var contentString2 = '<div>'+ 
       '<p><strong>name</strong></p>'+ 
       '</div>'; 
var infowindow2 = new google.maps.InfoWindow({ 
       content: contentString2 
      }); 
var contentString3 = '<div>'+ 
       '<p><strong>name</strong></p>'+ 
       '</div>'; 
var infowindow3 = new google.maps.InfoWindow({ 
       content: contentString3 
      }); 

var oldIcon = new google.maps.MarkerImage('images/maplogo.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50,50) 
    ); 
    var oldShadow = new google.maps.MarkerImage('images/mapshadow.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50, 50) 
    ); 
    var old = new google.maps.LatLng(50.714110196782485,-1.9864332675933838, 3); 
    var oldMarker = new google.maps.Marker({ 
     position: old, 
     map: map, 
     icon: oldIcon, 
     shadow: oldShadow, 
     title:"title", 
     animation: google.maps.Animation.DROP 
    }); 


    var hamIcon = new google.maps.MarkerImage('images/maplogo.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50,50) 
    ); 
    var hamShadow = new google.maps.MarkerImage('images/mapshadow.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50,50) 
    ); 
    var ham = new google.maps.LatLng(50.71584417870842,-2.006630301475525, 1); 

    var hamMarker = new google.maps.Marker({ 
     position: ham, 
     map: map, 
     icon: hamIcon, 
     shadow: hamShadow, 
     title:"title", 
     animation: google.maps.Animation.DROP 
    }); 

var centreIcon = new google.maps.MarkerImage('images/maplogo.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50,50) 
    ); 
    var centreShadow = new google.maps.MarkerImage('images/mapshadow.png', 
     new google.maps.Size(100,50), 
     new google.maps.Point(0,0), 
     new google.maps.Point(50,50) 
    ); 
    var centre = new google.maps.LatLng(50.71712636698597,-1.9832682609558105, 2); 
    var centreMarker = new google.maps.Marker({ 
     position: centre, 
     map: map, 
     icon: centreIcon, 
     shadow: centreShadow, 
     title:"title", 
     animation: google.maps.Animation.DROP 
    }); 

      google.maps.event.addListener(oldMarker, 'click', function() { 
    infowindow.open(map,oldMarker); 
    }); 
      google.maps.event.addListener(hamMarker, 'click', function() { 
    infowindow2.open(map,hamMarker); 
    }); 
      google.maps.event.addListener(centreMarker, 'click', function() { 
    infowindow3.open(map,centreMarker); 
    }); 
    } 

任何幫助,這將非常感謝,因爲我有一個截止日期,以滿足和JavaScript的理解很低。

感謝

回答

0

呼叫infowindow.close()click回調。

你應該只能夠用來替換你的聽衆:

google.maps.event.addListener(oldMarker, 'click', function() { 
    infowindow.open(map,oldMarker); 
    infowindow2.close(); 
    infowindow3.close(); 
}); 

google.maps.event.addListener(hamMarker, 'click', function() { 
    infowindow2.open(map,hamMarker); 
    infowindow.close(); 
    infowindow3.close(); 
}); 

google.maps.event.addListener(centreMarker, 'click', function() { 
    infowindow3.open(map,centreMarker); 
    infowindow.close(); 
    infowindow2.close(); 
}); 
+0

感謝Freejosh,這工作。 – user1848387

相關問題