I'm currently using the following code the place multiple markers on a Google Map using their API.
The problem I'm having is with multiple infowindows not working (only showing the last one).
There are loads of questions like mine here on SO. Actually make that a shitload of questions :-)
Just an example: Trying to bind multiple InfoWindows to multiple Markers on a Google Map and failing
The solution to my problem is kinda easy: just enclose the click listener to a (anonymous) function.
However what I don't understand is why my solution isn't working (saving the markers and infowindows in arrays instead of just one variable).
var markers = [];
var infowindows = [];
// add shops or malls
for (var key in data.markers) {
if (data.markers.hasOwnProperty(key)) {
infowindows[key] = new google.maps.InfoWindow({
content: data.markers[key].infowindow
});
markers[key] = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[key].location.lat, data.markers[key].location.lng),
map: map,
flat: true,
title: data.markers[key].name,
draggable: false
});
var iconFile = 'http://maps.google.com/mapfiles/ms/icons/'+marker_color+'-dot.png';
markers[key].setIcon(iconFile);
google.maps.event.addListener(markers[key], 'click', function() {
infowindows[key].open(map, markers[key]);
});
}
}
So... I don't what to get the solution how to get it working with some function to enclose the listener (although it should work, haven't tested it yet but will), but I want to know the reason why it wouldn't work if I add the markers and infowindows to arrays instead.