问题
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);
});
});
});
回答1:
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.
来源:https://stackoverflow.com/questions/29134470/google-maps-api-close-infowindow-when-clicking-somewhere-else