google maps - using multi-markers with multi info-windows -


i have array of info-windows corresponding array of markers. now, have problem, use below code generate info-window, when clicking on marker markers disappear unless one, when click on marker there no info-window come out. wrong code ? or shall ?

any answer appreciated, in advance.

this code inside loop:

infowindoarray[i][j] = new google.maps.infowindow({                       content:"lat: "+this.position.lat() + "\nlng: " + this.position.lng() + "\n"+ this.custominfo,                 });                  google.maps.event.addlistener(allmarkers[i][j], 'click', (function(x) {                     return function() {infowindoarray[i][j].open(map,allmarkers[i][j]);}                 })(x)); 

edited: added whole loop more clear:

        (var = 0; < arrayofallfilesdata.length-1; i++) {//to select file.             var myarr = arrayofallfilesdata[i+1];              allmarkers[i] = new array();             (var j=0; j < myarr.length; j++) {                 var marker_center = new google.maps.latlng(myarr[j][0],myarr[j][1]);                  allmarkers[i][j] = new google.maps.marker({                     position: marker_center,                     custominfo: "number of encounters: "+myarr[j][2],                     title:'click zoom',                     visible:true,                 });                 allmarkers[i][j].setmap(map);              };         };     } 

here's compromise approach 1 infowindow per maker infowindows not created in advance. instead, created on-demand, each marker clicked.

this way, infowindows never seen not created. , once created, each infowindow reused if user revisits marker.

outside nested i/j loops :

var allmarkers = [];//not necessary unless used elsewhere  function clickmarker_closure(arr) {     return function() {         if(!this.infowin) {             this.infowin = new google.maps.infowindow({                 content: [                     "lat: " + this.position.lat(),                     "lng: " + this.position.lng(),                     "number of encounters: " + arr[2]                 ].join("\n")             });         }         this.infowin.open(map, this);     }; } 

and nested i/j loops :

for (var i=0; i<arrayofallfilesdata.length-1; i++) {//to select file.     var myarr = arrayofallfilesdata[i+1],         marker;     allmarkers[i] = [];//necessary?     (var j=0; j < myarr.length; j++) {         marker = new google.maps.marker({             map: map,             position: new google.maps.latlng(myarr[j][0], myarr[j][1]),             title: 'click zoom',             visible: true,         });         allmarkers[i][j] = marker;//necessary?         google.maps.event.addlistener(marker, 'click', clickmarker_closure(myarr[j]));     } } 

users unaware infowindows created on-demand.

edit 1

  • edited above better knowledge of structure of i/j loops , simplify closure's reference data , marker.
  • infowindoarray no longer required.

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -