javascript - Marker click event always opens to the last pushed infowindow -
javascript - Marker click event always opens to the last pushed infowindow -
i have json object infocentros
utilize build map, this:
for ( var = 0; < infocentros.length; i++ ) { var centro = infocentros[i]; var lat = centro.cordenadas.lat; var lon = centro.cordenadas.long; if (lat && lon) { c++; latlon = new google.maps.latlng(lat, lon); var moptions = { position: latlon, map: $project.gmap } moptions.icon = theme_uri + '/images/marker.png'; var marker = new google.maps.marker(moptions); $project.mapmarkers.push(marker); google.maps.event.addlistener(marker, 'click', function() { $project.mapinfowindow.setcontent( '<div class="sescam-info-window">' + '<h3>' + centro.nombre + '</h3>' + '<p>' + centro.lugar + '</h3>' + '<p>coordinador</p>' + '<p>' + centro.coordinador.nombre + '</p>' + '<p>' + centro.coordinador.email + '</p>' + '<p>responsable</p>' + '<p>' + centro.responsable.nombre + '</p>' + '<p>' + centro.responsable.email + '</p>' + '</div>' ); $project.mapinfowindow.open($project.gmap, marker); }); $project.mapbounds.extend(latlon); } }
it seems work fine, if have 5 markers, doesn't matter 1 click, infowindow corresponds lastly item (position , content),
any thought missing? thought passing marker
addlistener trick..
problem 3rd argument of google.maps.event.addlistener. anonymous function contains variable parent scope when evaluated marker click take lastly value assigned. closure more details. may able desired behaviour using "bind" feature of javascript function prototype in below manner:
//inside loop google.maps.event.addlistener(marker, 'click', handlemarkerclick.bind(undefined, marker, i)); //other codes if any...
defining handleclick
function handlemarkerclick(marker, index) { if (typeof infowindow === 'undefined') { infowindow = new google.maps.infowindow({}); } var info = infocentros[index]//helpful info //create content dynamic info infowindow.setcontent("dynamic content"); infowindow.open(marker.getmap(), marker);//modify per requirement }
javascript google-maps google-maps-api-3 infowindow marker
Comments
Post a Comment