leafletjs how to get a handle to the active pop/marker

吃可爱长大的小学妹 提交于 2019-12-02 10:41:17

You would have several options to keep a reference to the popup in which the clicked button is contained.

They "easy" one (for your case where you fill the popup content with plain HTML text, including your button HTML) would be to keep a mapping of each popup / marker, with the keys being a kind of ID, then pass that ID to your weatherload function.

If you keep a reference to the marker only, you can retrieve the bound popup using getPopup() method.

var allMarkersMap = {};
var currentID = 0;

// Create a marker with a popup.
var button = '<button onclick="weatherload('+ lat + ',' + lng + ',' + currentID + ')">Click Me for Weather!</button>';
var marker = L.marker(lat, lng).bindPopup(permanentContent + button);
marker.myPermanentContent = permanentContent;

allMarkersMap[currentID] = marker;
currentID += 1;

// Loop to create more markers.

function weatherload(lat, lng, markerID) {
  var marker2 = allMarkersMap[markerID];
  var permanentContent2 = marker2.myPermanentContent;
  var popup = marker2.getPopup();
  // Use this to add your loading GIF. Do the same in your AJAX callback.
  popup.setContent(permanentContent2 + newContent);
}

A more complex solution (but which gives you more control) would be to create the HTML elements that will be in your popup. That way, you can directly bind your popup as a property of your button.

Demo: http://jsfiddle.net/ve2huzxw/95/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!