0

I want to close the current infowindow when I click on any next marker, for now it shows all infowindows after clicking.

My JavaScript

var marker = new MarkerWithLabel({
    position: latlng,
    draggable: false,
    raiseOnDrag: true,
    map: map,
    icon: 'a.png',
    labelContent: '<?php echo $rating.' % Review '.$review;?>',
    labelAnchor: new google.maps.Point(22, 0),
    labelClass: "labels"
});

marker['infowindow'] = new google.maps.InfoWindow({
    content: '<p><?php echo $name ;?></p>'
});


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

    this['infowindow'].open(map, this);
});
MrUpsidown
  • 20,698
  • 12
  • 69
  • 122
Akhilendra
  • 1,075
  • 1
  • 14
  • 29
  • 2
    If you only ever want one infowindow open, only create one, move it to each clicked marker. Possible duplicate of [Google Maps JS API v3 - Simple Multiple Marker Example](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example) – geocodezip May 03 '16 at 12:49

1 Answers1

4

Save previously opened infowindow in a variable and then close when a new window opens.

var currWindow =false; 

base.attachInfo = function(marker, i){
    var infowindow = new google.maps.InfoWindow({
        content: "Content"
    });

    google.maps.event.addListener(marker, 'click', function(){
        if( currWindow ) {
           currWindow.close();
        }

        currWindow = infowindow;
        infowindow.open(base.map, marker);
    });
}
Jenson M John
  • 5,187
  • 4
  • 26
  • 46
  • Rather than doing that, I suggest you look at @geocodezip comment on your question. – MrUpsidown May 04 '16 at 11:12
  • 1
    Thanks! I don't know why someone downvoted it before, this is the correct answer! I have been trying to do this for the last 2 hours (waste of time), solved now thanks to your answer! – Notorious Apr 12 '17 at 07:06