2012-08-10 151 views
1

我不知道該怎麼辦......我只想關閉所有打開的infoBubbles,如果我要打開一個新的。我正在使用下面的代碼。想法?我嘗試了很多並且搜索了很多,但它不應該如此複雜,應該如此嗎?我想創建一個數組併爲開放的ID保存ID,但我認爲必須有另一個更簡單的方法來解決這個問題。打開一個新的(gMap v3)時關閉打開InfoBubble

$(document).ready(function(){ 
      createmap(); 

    function createmap(lat,lng){ 
     var latlng = new google.maps.LatLng(50, 10); 
    var myOptions = { 
    zoom: 12, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
    bounds = new google.maps.LatLngBounds(); 
      createMarker(); 
    } 

    function createMarker(){ 
      var markers = []; 
      var cm = window.cm = new ClusterManager(
       map, 
       { 
        objClusterIcon: new google.maps.MarkerImage('images/map/flag_cluster.png', false, false, false, new google.maps.Size(20,20)), 
        objClusterImageSize: new google.maps.Size(20,20) 
       } 
      ); 

      var json = []; 
      var x1 = -85; 
      var x2 = 85; 
      var y1 = -180; 
      var y2 = 180; 
      for (var i=0; i<20; ++i) { 
       json.push(
        '{'+ 
         '"longitude":'+(x1+(Math.random()*(x2-x1)))+','+ 
         '"latitude":'+(y1+(Math.random()*(y2-y1)))+','+ 

         '"title":"test"'+ 
        '}' 
       ); 
      } 
      json = '['+json.join()+']'; 
      // eval is ok here. 
      eval('json = eval(json);'); 

      infos = []; 
      $.each(json, function(i,item){  
    var contentString = '<div id="content"><a onclick="createdetailpage('+i+');">'+item.title+'<br /></a></div>'; 

       console.log(item.latitude); 
       var myLatlng = new google.maps.LatLng(item.latitude,item.longitude); 
      var marker = new google.maps.Marker({ 
        position: myLatlng, 
        //map: map, 
        flat: true, 
        title:item.title, 
        //animation: google.maps.Animation.DROP, 
        //icon: myIcon 
       }); 
       var infoBubble = new InfoBubble({    
        content: '<div class="phoneytext">'+contentString+'</div>' 
       });         

       google.maps.event.addListener(marker, 'click', function() { 
        if (!infoBubble.isOpen()) { 
         infoBubble.open(map, marker); 
        } 
       }); 
       cm.addMarker(marker, new google.maps.Size(50, 50));     
      }); 
     map.fitBounds(bounds);    
    } 
    }); 

回答

0

InfoBubbleOverlayView。據我所知,添加疊加層時會發出一個overlaycomplete事件。也許你所有的InfoBubble都可以聽到該事件,如果添加的覆蓋圖與「this」不同,則關閉。

這只是一個想法,但我沒有嘗試過自己。反饋讚賞,如果你嘗試一下!