2011-06-17 80 views
3

我不能爲我的生活獲取只有一個infoWindow在V3的API中顯示。我需要一個在下一場比賽開始前關閉。還希望在地圖上的任何位置關閉infoWindow。這是否在初始化函數內進行?關閉InfoWindow - 關閉onclick - Google地圖

這裏是我完整的腳本:

// defining vars 
var map = null; 
var markers = []; 
var adUnit; 
var center = new google.maps.LatLng(39.676502,-105.162101); 

// function that soley initializes the new map object 
function initialize() { 

    var mapOptions = { 
     zoom: 10, 
     center: center, 
      mapTypeControl: true, 
      mapTypeControlOptions: { 
       style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, 
      }, 
      zoomControl: true, 
        zoomControlOptions: { 
         style: google.maps.ZoomControlStyle.SMALL, 
         position: google.maps.ControlPosition.LEFT_BOTTOM 
        }, 
      panControl: false, 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
    } 
    map = new google.maps.Map($('#map_canvas')[0], mapOptions); 
    // call the loader function 
    loadXML(); 

    // Adsense Options         
    var adUnitDiv = document.createElement('div'); 
    var adUnitOptions = { 
     format: google.maps.adsense.AdFormat.BUTTON, 
     position: google.maps.ControlPosition.RIGHT_BOTTOM, 
     publisherId: 'ca-google-maps_apidocs', 
     map: map, 
     visible: true 
    }; 
    adUnit = new google.maps.adsense.AdUnit(adUnitDiv, adUnitOptions);  

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


} //<-- Initialize 

// load (and map) the markers from XML data 
function loadXML() { 
    $.get('data.xml', function(data) { 
     // var bounds = new google.maps.LatLngBounds(); //<--- Size map to bounds of markers LatLng 
     $(data).find('marker').each(function() { 

      var lat = $(this).attr('lat') 
      var lng = $(this).attr('lng') 
      var name = $(this).attr('name') 
      var type = $(this).attr('type') 

      var LatLng = new google.maps.LatLng(parseFloat($(this).attr('lat')), 
            parseFloat($(this).attr('lng'))); 

      var myToggleData = {     //<---- collecting data to be toggled on/off 
       type  : $(this).attr('type'), 
       skill   : $(this).attr('skill'), 
       eta_whatever_that_means : parseFloat($(this).attr('eta')) 
      }   

      var marker  = new google.maps.Marker({ 
       position : LatLng, 
       map   : map, 
       icon  : 'images/marker.png', 
       title  : $(this).attr('name'), 
       data  : myToggleData 
      }); 

       var html ='<div id="winBackground">   <div class="winTitle">' + name + '</div>   <div class="winHead">Type: <div class="winData">' + type + '</div></div> </div>'; 


       // create the infoWindow 
       var infoWindow = new google.maps.InfoWindow(); 
       // add a listener to open the infoWindow when a user clicks on one of the markers 
       google.maps.event.addListener(marker, 'click', function() { 
       infoWindow.setContent(html); 
       infoWindow.open(map, marker); 
       }); 

      markers.push(marker); 
      // bounds.extend(LatLng); 
     }); 


     // map.fitBounds(bounds); 
    }); 
} 
// The toggleMarker function 
function toggleMarkers(attr,val) { 
    if (markers){ 
     for (i in markers) { 
      if(markers[i].data[attr] == val){ 
       var visibility = (markers[i].getVisible() == true) ? false : true; 
       markers[i].setVisible(visibility); 
      } 
     } 
    } 
} 

回答

6

不要爲每一個標誌一個新的信息窗口。只有建立一個信息窗口,然後當你改變它,請執行下列操作:

infowin.setContent(""); 
infowin.open(map, marker); 
2

這將關閉在地圖上點擊所有信息窗口(而不需要有信息窗口對象的引用)。

google.maps.event.addListener(map, 'click', function() { 
    infowindow.open(null, null); 
}); 
0

嘗試這樣:

if(popup) { 
    popup.close(); 
} 

popup = new google.maps.InfoWindow({ 
    content:'My New InfoWindow', 
}); 

popup.open(map, marker); 
2
google.maps.event.addListener(marker, 'click', function() { 
     if(!marker.open){ 
      infowindow.open(map,marker); 
      marker.open = true; 
     } 
     else{ 
      infowindow.close(); 
      marker.open = false; 
     } 
     google.maps.event.addListener(map, 'click', function() { 
      infowindow.close(); 
      marker.open = false; 
     }); 
    }); 

試試這個它會工作

相關問題