Hello,
first I must say that Esa's example is really nice!
I have another suggestion using multiple instances of InfoWindows (one
for each marker), using a global variable to keep record of the
current displayed info window.
code snippet:
// global var to keep record of the currently displayd info
window
var currentInfoWindow = null;
// (...) other code
// create marker (markers are numbered marker0, marker1,
marker2 ...)
var markerLatLng = new google.maps.LatLng(52.02235, 8.54339);
var marker0 = new google.maps.Marker({
position: markerLatLng,
title: 'Ravensberger Spinnerei',
icon: '
http://google-maps-icons.googlecode.com/files/museum-
historical.png',
map: map
});
// create instance of InfoWindow (numbered infowindow0,
infowindow1, infowindow2 ...)
var infowindow0 = new google.maps.InfoWindow({
content: 'Ravensberger Spinnerei'
});
// create event listener for the marker
google.maps.event.addListener (marker0, 'click', function() {
// and here it comes:
// check if there is already an InfoWindow displayed
(currentInfoWindow is not null). if yes, close it
if (currentInfoWindow != null) { currentInfoWindow.close(); }
// show new info window
infowindow0.open(map, marker0);
// store the now displayed info window in global var
currentInfoWindow
currentInfoWindow = infowindow0;
});
If you want to remove the currently displayed info window when you
click somewhere on the map ("click outside") too you have to add an
event listener for the map
google.maps.event.addListener(map, 'click', function() { if
(currentInfoWindow != null) { currentInfoWindow.close(); } });
Hope this helps