javascript - Adding info windows to multiple circle overlays -


i having trouble adding info window each 1 of multiple circle overlays. when click on circle info window appears, not on circle clicked rather on last circle added. seems each time loop executes event listener added every circle content of info window derived last circle added.

for (i in citypoints) {     var magnitudeoptions = {         map: map,         center: citypoints[i].center,         radius: citypoints[i].magnitude,         id:citypoints[i].id,         addr:citypoints[i].addr,         infowindowindex:      };      citycircle = new google.maps.circle(magnitudeoptions);      circlesarray.push(citycircle);      infowindow = new google.maps.infowindow({ content: citypoints[i].id + " " + citypoints[i].addr });      infowindowsarray.push(infowindow);      google.maps.event.addlistener(circlesarray[i], 'click', function (ev) {          infowindowsarray[i].setposition(citypoints[i].center);          infowindowsarray[i].open(map);      });  } 

that's because parameter pass last, always. try this

<!doctype html> <html>   <head>     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">     <meta charset="utf-8">     <title>circles</title>     <style>     #map-canvas {     height: 500px;     width: 500px;     </style>     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>     <script>  // create object containing latlng, population. var citypoints = {}; citypoints[0] = {   center: new google.maps.latlng(41.878113, -87.629798),   id: 0,   addr: 'avenue0',   magnitude: 100000 }; citypoints[1] = {   center: new google.maps.latlng(40.714352, -74.005973),   id: 1,    addr: 'avenue1',   magnitude: 100000 }; citypoints[2] = {   center: new google.maps.latlng(34.052234, -118.243684),   id: 2,   addr: 'avenue2',   magnitude: 100000 } var citycircle; var infowindow = new google.maps.infowindow();    function initialize() {   var mapoptions = {     zoom: 4,     center: new google.maps.latlng(37.09024, -95.712891),     maptypeid: google.maps.maptypeid.terrain   };    var map = new google.maps.map(document.getelementbyid('map-canvas'),       mapoptions);  (i in citypoints) {            var magnitudeoptions = {                 map: map,                 center: citypoints[i].center,                 radius: citypoints[i].magnitude,                 id:citypoints[i].id,                 addr:citypoints[i].addr,                 infowindowindex:             };    citycircle = new google.maps.circle(magnitudeoptions);      google.maps.event.addlistener(citycircle, 'click', (function(citycircle, i) {         return function() {             infowindow.setcontent(citypoints[i].id + " " + citypoints[i].addr);             infowindow.setposition(citycircle.getcenter());             infowindow.open(map);         }       })(citycircle, i)); } } google.maps.event.adddomlistener(window, 'load', initialize);      </script>   </head>   <body>     <div id="map-canvas"></div>   </body> </html> 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -