jquery - Common closure issues for looping markers in Google Map API -


i beginner write webpage , show markers in google map infowindows. however, found infowindow show information of last records. reference previous article in stackoverflow, found problem caused common closure problem in javascript find difficult understand how fix problem in coding. can 1 help?

besides, ask 1 simple html question. how create href if pdf name variable shown in coding. many thanks!!!

var showall=function() {         var url2="show_all_trig.php";         $.ajax({             url: url2,             type: "post",             datatype: "json",             success: function(data){                 $.each(data, function(i, item) {                      station_num = item.station_num;                       trig_name = item.trig_name;                      loc_x = item.loc_x;                      loc_y = item.loc_y;                      loc = new google.maps.latlng(loc_x, loc_y);                          var trigicon = 'images/start2.png';                      marker = new google.maps.marker({                      map: map,                      position: loc,                      icon: trigicon,                      title: trig_name                      })                      markersarray.push(marker)                      html = "<b>trig. station name: </b>" + trig_name + "<br/> <b>station number: </b>" + station_num + "<br/> <b>sketch: </b>" +"<a target='_blank' href= 'summarysheet/'+ 'station_num +'.pdf'>station_num</a>";                       google.maps.event.addlistener(marker, 'click', function() {                         //alert (html);                         infowindow.setcontent(html);                         infowindow.open(map, marker);                         });                 });              },              error:function(xhr, ajaxoptions, thrownerror){                  alert(xhr.status);                  alert(thrownerror);               }          }); } 

here's technique you're looking for:

(function(temp_html,temp_map,temp_marker) {   google.maps.event.addlistener(temp_marker, 'click', function() {     infowindow.setcontent(temp_html);     infowindow.open(temp_map, temp_marker);   }); }(html,map,marker)); 

in short, you're doing here (a) creating anonymous function 3 arguments, (b) passing html, map , marker in values arguments, , (c) executing anonymous function. temp_ variables enclosed in anonymous function , invisible surrounding code, hence name "closure."

(in case, looks map global variable don't really need pass closure. it's not bad thing anyway.)


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -