javascript - Leaflet: how to affect all markers on a map? -
basically, have several markers on leaflet map. using jquery. @ moment, if want fill marker's popup ajax, have use code:
var marker = l.marker([51.5, -0.09]).addto(map); var marker2 = l.marker([51.49, -0.09]).addto(map); marker.on("click", function () { $.ajax({ type: "get", url: "home/transactionform", datatype: "html", success: function(ajaxresult) { marker.setpopupcontent(ajaxresult); }, error: function (ajaxresult) { alert("filling popup failed!"); } }); }); marker2.on("click", function () { $.ajax({ type: "get", url: "home/transactionform", datatype: "html", success: function(ajaxresult) { marker2.setpopupcontent(ajaxresult); }, error: function (ajaxresult) { alert("filling popup failed!"); } }); });
obviously, far ideal. in jquery, simple:
$("#map").on("click", "#marker", function() { $.ajax({ type: "get", url: "home/transactionform", datatype: "html", success: function(ajaxresult) { $(this).setpopupcontent(ajaxresult); }, error: function (ajaxresult) { alert("filling popup failed!"); } });
how do this? know map's id, can't figure out id leaflet assigns markers, popups , other elements.
leaflet doesn't add different ids each marker/popups. adds appropriate class each element. elements not meant accessed jquery selectors.
if want set popup content ajax same url (as doing here), why not add markers list, iterate through , set event handler each marker in list. this:
var markersarray = []; markersarray.push(marker1); markersarray.push(marker2); (var marker in markersarray) { marker.on("click", function () { $.ajax({ type: "get", url: "home/transactionform", datatype: "html", success: function(ajaxresult) { marker.setpopupcontent(ajaxresult); }, error: function (ajaxresult) { alert("filling popup failed!"); } }); }); }
Comments
Post a Comment