google-mapsinfobubble

Show only one infobubble "at a time" in google maps API 3


I am trying to make it so that only one infobubble shows at a time, in other words to toggle infobubble. The marker gets assigned this event handler in a for loop, looping through the marker array.

at the moment this works in a way that opens the infobubble but never closes it. I can click multiple markers and each will have the infobubble with out closing the previous one

Iv tried adding infobuble2.close(); but the closes iv got with it is i would have to click on the markers twice to close the previous infobubbles.

Iv also tried testing if the infobubble is open using infoBubble2.isOpen() it appears to only be false.

And yes if your wondering i have tried infowindow, and the functionality works well using it, but because of design constrains i need to style the popup accordingly below is my code

  var infoBubble2;

  google.maps.event.addListener(marker, 'click', function() {

             infoBubble2 = new InfoBubble({
                map: map,
                position: new google.maps.LatLng(-35, 151),
                shadowStyle: 1,
                padding: 0,
                backgroundColor: 'rgb(255,255,255)',
                borderRadius: 4,
                arrowSize: 10,
                borderWidth: 1,
                borderColor: '#2c2c2c',
                disableAutoPan: true,
                hideCloseButton: !0,
                arrowPosition: 30,
                backgroundClassName: 'phoney',
                arrowStyle: 2
              });

            infoBubble2.setContent("some content");
            infoBubble2.open(map, marker);
  });

Solution

  • I solved it by declaring the infobubble2 variable outside the initialize() function and then assigning it the value of new infobubble(); within the initialize(), then the onclick event which is assigner to each marker opens and populates the content of the infobubble.

    Below is what i did. Thanks for the help everyone.

    var infoBubble2;
    
    function initialize() {
        var myOptions = {
              zoom: zoom,
              minZoom: 0, maxZoom: 20,
              center: myLatLng,
              mapTypeId: maptype,
              mapTypeControl:false,
              streetViewControl:false,
              panControl:false
          };
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
       infoBubble2 = new InfoBubble({
                map: map,
                position: new google.maps.LatLng(-35, 151),
                shadowStyle: 1,
                padding: 0,
                backgroundColor: 'rgb(255,255,255)',
                borderRadius: 4,
                arrowSize: 10,
                borderWidth: 1,
                borderColor: '#2c2c2c',
                disableAutoPan: true,
                hideCloseButton: !0,
                arrowPosition: 30,
                backgroundClassName: 'phoney',
                arrowStyle: 2
              });
    
          $.getJSON('include/jsonCatPoints.php', function(data) {
              for(var i=0; i<data.length;i++){
                placeMarker(data[i]['cat_lat'], data[i]['cat_long'], map);
              }
            });
    
    
    }//ends initialize
    
    function placeMarker(lat, longg, map) {
          var image = 'img/cat_marker.png';
             marker = new google.maps.Marker({
              position: new google.maps.LatLng(lat,longg),
              map: map,
              icon: image
            });
          makeBubble(map, "test" +lat, marker);
           markers.push(marker);
      }//ends placeMarker
    
    
    function makeBubble(map, contentString, marker) {
      google.maps.event.addListener(marker, 'click', function() {
            infoBubble2.setContent("message"+contentString);
            infoBubble2.open(map, marker)
     });
    }// ends makeBubble