Hi,
I am currently learning Google Maps API V3.
I want to display an infowindow for each marker on a map and I have written two codes snippets doing the same thing, yet producing completely different results.
Version A: Successfully displays an infowindow for each marker.
I tried to do it another way but
Version B: Displays infowindow for only last marker in Array
Please, I spend the last 6 hours trying to understand what I did wrong. Can you help me please?
The is Version A
infowindow = new google.maps.InfoWindow({
content: content
});
var lastIndex = myJSONobject.geolocation.length - 1;
for(i=0;i<myJSONobject.geolocation.length;i++) {
if (i == lastIndex) {
placeIcons(myJSONobject.geolocation[i]);
} else {
addMarker(myJSONobject.geolocation[i]);
}
}
function addMarker(data) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.lon),
map: map,
title: data.time_inserted
});
google.maps.event.addListener(marker, "click", function() {
openInfoWindow(marker);
});
}
This is Version B and should do the same thing as above.
But it only displays an infowindow for the last marker.
var lastIndex = myJSONobject.geolocation.length - 1;
for(i=0;i<myJSONobject.geolocation.length;i++) {
if (i == lastIndex) {
placeIcons(myJSONobject.geolocation[i]);
} else {
//addMarker(myJSONobject.geolocation[i]);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(myJSONobject.geolocation[i].lat, myJSONobject.geolocation[i].lon),
map: map,
title: myJSONobject.geolocation[i].time_inserted
});
google.maps.event.addListener(marker, "click", function() {
openInfoWindow(marker);
});
}
}