2012-08-10 63 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”不同,则关闭。

这只是一个想法,但我没有尝试过自己。反馈赞赏,如果你尝试一下!