I have a shared infoWindow
for all my markers. It works good if I use jquery's $().each(function(){})
, but if I change it to JavaScrips's native for or while loop, it's not working as expected.
Whenever I click a marker, it will open the last populated marker's infoWindow
, instead of the clicked marker's infoWindow
.
This works:
$(stores).each(function() {
var storeId = $(this).attr('storeId');
var address = $(this).attr('rawAddress');
var city = $(this).attr('city');
var state = $(this).attr('state');
var zip = $(this).attr('zip');
var lat = $(this).attr('lat');
var lng = $(this).attr('lng');
var distance = $(this).attr('distance');
//create marker
var point = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({ position: point
, map: InStorePickup.map
, title: city + ' #' + storeId
});
//create infowindow content
var infoWindowContent = '<div style="font-size: 8pt;">'
+ '<strong>' + city + ' #' + storeId + '</strong> (' + parseFloat(distance).toFixed(2) + 'mi)<br />'
+ address + '<br />' + city + ', ' + state + ' ' + zip
+ '</div>';
google.maps.event.addListener(marker, 'click', function() {
InStorePickup.openInfoWindow(marker, infoWindowContent);
});
bounds.extend(point);
});
But this does not work:
for (var i = 0; i < 3; i++) {
var store = stores[i];
var storeId = $(store).attr('storeId');
var address = $(store).attr('rawAddress');
var city = $(store).attr('city');
var state = $(store).attr('state');
var zip = $(store).attr('zip');
var lat = $(store).attr('lat');
var lng = $(store).attr('lng');
var distance = $(this).attr('distance');
//create marker
var point = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({ position: point
, map: InStorePickup.map
, title: city + ' #' + storeId
});
//create infowindow content
var infoWindowContent = '<div style="font-size: 8pt;">'
+ '<strong>' + city + ' #' + storeId + '</strong> (' + parseFloat(distance).toFixed(2) + 'mi)<br />'
+ address + '<br />' + city + ', ' + state + ' ' + zip
+ '</div>';
google.maps.event.addListener(marker, 'click', function() {
InStorePickup.openInfoWindow(marker, infoWindowContent);
});
bounds.extend(point);
}
Any thoughts?