javascript - Creating dynamic links with leaflet onclick event -


im having trouble creating links separate dynamic page, user directed page when click marker created leaflet. code below entirely works; however, when marker clicked instead of going relevant page, markers link last link generated loop


//add marker map on results page each result function addmarker(markerarray){      var dynamicname = 'marker';     var dynamicnumb = 'numb';     //create empty list hold each marker     var markerlist = [];     //for each result creater marker , push markerlist     (i = 0; < markerarray.length; i++) {         //turn park id integer         var toint = parseint(markerarray[i][2]);         //generate marker in form l.marker(latitude, longitude, name of park shown on mouse over, link individual item page on click).add marker mymap         this[dynamicname+i] = l.marker([markerarray[i][0], markerarray[i][1]], {title: markerarray[i][3]}).on('click', function(e) {markerurl(toint);}).addto(mymap);         //place marker in list         markerlist.push(this[dynamicname+i]);     }     //create feature group of markers in markerlist     var group = new l.featuregroup(markerlist);     //auto scale map fit markers     mymap.fitbounds(group.getbounds().pad(0.2)); }  //create dynimic link individual items page specified in parameter itemsid function markerurl(itemsid){     window.location.href = 'items.php?parkid=' + itemsid; } 

closures, closures, closures.

function addmarker(markerarray){     (i = 0; < markerarray.length; i++) {         var toint = parseint(markerarray[i][2]);          this[dynamicname+i] = l.marker(             [markerarray[i][0], markerarray[i][1]],              {title: markerarray[i][3]}         ).on('click', onmarkerclick(toint)).addto(mymap);  }  function onmarkerclick(itemsid){     return function(ev) {         window.location.href = 'items.php?parkid=' + itemsid;     } } 

see leaflet marker event fires @ wrong time , pretty duplicate of question.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -