I am trying to add markers to google maps using V3. I have my places in json object (places):
var num_places = places.length; for(var i = 0; i < num_places; i++) { place_lat_lng = new google.maps.LatLng(places[i].lat, places[i].lng); var infowindow = new google.maps.InfoWindow({ content: '<h2>' + places[i].name + '</h2><p>' + places[i].body + '</p>' }); var marker = new google.maps.Marker({ position: place_lat_lng, map: mymap, title: places[i].name, zIndex: i }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(mymap, marker); }); }
The code inserts markers, but when I click on any of them, the info show is shown (and moves the map) always to the last marker in the list.
I tried using an array for infoWindow:
var infoWindow = new Array(); for(var i = 0; i < num_places; i++) { [...] var infowindow[i] = new google.maps.InfoWindow({ content: '<h2>' + places[i].name + '</h2><p>' + places[i].body + '</p>' }); [...] google.maps.event.addListener(marker, 'click', function() { infowindow[i].open(mymap, marker); }); }
But nothing changes.
Where am I mistaken?
Strae source share