0

I have tried google it but can't find the one to apply to my code. i want my infowindow(s) to close everytime someone presses another place on the map or opens another infowindow somewhere else.

Can someone please help?

var myLatlng = new google.maps.LatLng(val.coords.split(',')[0].trim(),val.coords.split(',')[1].trim());

var infowindow = null;
        var contentString = '<div id="content">'+
  '<div id="siteNotice"> content'+
  '</div>';


        var infowindow1 = new google.maps.InfoWindow({
            content: contentString,
            maxWidth: 250
        });

        var marker1 = new google.maps.Marker({
            position: myLatlng,
            map: map,
            icon: image,
        });

        google.maps.event.addListener(marker1, 'click', function() {
            infowindow1.open(map,marker1);
        });


    });
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Peter
  • 287
  • 2
  • 5
  • 13
  • I've been trying to build up a jsfiddle to test my response, without luck. Can you post enough code so I can build up a test base (or better yet, you build up a JSfiddle to help us all understand your issue, fix it and share the fix with the rest of the world...) – zipzit Mar 19 '15 at 00:01
  • I still can't get this thing to load up... but I do see a problem with your closing brackets.. You've got too many that don't match up. – zipzit Mar 19 '15 at 00:35
  • Possible duplicate of [Closing info windows in google maps by clicking the map?](https://stackoverflow.com/questions/10022873/closing-info-windows-in-google-maps-by-clicking-the-map) – Vivek Buddhadev Oct 11 '17 at 08:50

1 Answers1

3

Checkout the code at JS Fiddle I added

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

Obviously to catch all the open items on the map, you'd have to use a small loop to catch 'em all within that function, but that's something I'll leave to you. I would add a variable to keep track of how many you've created, then loop "infowindow"+var_index to that max value, set 'em all to close. I'm still looking for a better way to grab all open infowindows, but in the meantime this technique will work.

zipzit
  • 3,778
  • 4
  • 35
  • 63
  • 1
    This is nice, but if you have multiple markers, you can click on several of these without closing the original marker. Any idea how to include these other markers within the listener so that when you have one infoWindow open and another one is clicked, the first one closes? – Brad Ahrens Sep 06 '17 at 11:37