javascript - Using events on dynamic markers -


i have init function in javascript:

inicia2 = function(){ console.log("google maps init")       //variables declaration         var lat,long,         cordenadasclientes = clientes.find({}, {fields:{'metadata.latcliente':           1,'metadata.longcliente':                       1,'metadata.nombrecliente':1}}).fetch();      //getting geolocation        lat = session.get('lat');        long = session.get('lon');      //map options        var mapoptions = {       center: new google.maps.latlng(lat,long),       zoom: 15,       disabledefaultui: true,       styles:[{"stylers":[{"hue":"#ff1a00"},{"invert_lightness":true},{"saturation":-100},{"lightness":33},{"gamma":0.5}]},{"featuretype":"water","elementtype":"geometry","stylers":[{"color":"#2d333c"}]}],       maptypeid: google.maps.maptypeid.roadmap     };     //initialize map         var map = new google.maps.map(document.getelementbyid("mapa_general"),            mapoptions);     //geolocation marker           var marker = new google.maps.marker({             position: new google.maps.latlng(lat,long),             map: map,            title: 'segun nosotros tu te encuentras aqui!',            animation: google.maps.animation.drop,          con:'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'       });     //dinamyc markers , info window       var texto = "dale pa ",infowindow,marker2,markers = [];        for(var i=0;i<cordenadasclientes.length;i++){           var latitudcliente = cordenadasclientes[i].metadata.latcliente;           var longitudcliente = cordenadasclientes[i].metadata.longcliente;           var nombrecliente = cordenadasclientes[i].metadata.nombrecliente;      //dinamic marker         marker2 = new google.maps.marker({           position: new google.maps.latlng(latitudcliente ,longitudcliente),           map: map,           title: nombrecliente,          icon :'http://maps.google.com/mapfiles/marker_yellow.png',      });      //dinamic infowindow         infowindow = new google.maps.infowindow({           content: texto + nombrecliente      });     //populatin markers array      markers.push(marker2);        } //closing // seems problem           google.maps.event.addlistener(markers, 'click', function() {         infowindow.open(map,markers);     });  } 

i need markers[i] inside google.maps.event.addlistener, functions not allowed inside loop.

the map works fine, creating markers (geo markers , client markers).

i'm rendering map this:

template.mapageneral.rendered = function(){       inicia2(); } 

this image:

map

the yellow markers created dynamically.

solution: adding parameter, each infowindow has different content according each client on mongo collection , using loop

so function looks this:

    function myinfowindow(marker2,map,nombrecliente){        var infowindow = new google.maps.infowindow();         google.maps.event.addlistener(marker2, 'click', function() {           for(var i=0;i<cordenadasclientes.length;i++){           infowindow.setcontent("dale pa " + nombrecliente);           infowindow.open(map, marker2);       }});   } 

try using this:

after markers.push(marker2);use myinfowindow(marker2,map); , add function code:

function myinfowindow(marker2,map){// method bind infowindow marker. var infowindow = new google.maps.infowindow();  google.maps.event.addlistener(marker2, 'mouseover', function() {     infowindow.setcontent("hi it's infowindow");     infowindow.open(map, marker2);     }); google.maps.event.addlistener(marker2, 'mouseout', function() {infowindow.close();}); } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -