我正在使用 leaflet.js 在 OSM 地图上显示标记。
问题是,第一次单击标记时,弹出窗口会正常打开,但第二次单击同一标记时,弹出窗口将不再打开。
PS:代码中的任何其他地方我都会关闭弹出窗口(使用
closePopup()
函数)。在下面的块中,我什至注释掉了单击标记后其他弹出窗口的显式关闭。
PPS:我的应用程序在 Ruby on Rails(ruby-1.9.3、Rails 3.2.16)上运行,并使用 leaflet-rails(0.7.2)
bindListeners = function(marker){
marker.on('click', function(evt) {
// resize all markers' icons to default size
for (i=0; i<markersOfTheMap.length; i++) {
resizeMarkerIcon(markersOfTheMap[i], false);
}
//map.closePopup();
var infoBoxContent = buildInfoboxHtml(marker);
marker.bindPopup(infoBoxContent, {className: 'click-popup'}, {closeOnClick: false});
resizeMarkerIcon(marker, true);
marker.openPopup();
var popup = marker.getPopup(); // returns marker._popup
popup._isOpen = true;
console.log("is popup open? " +popup._isOpen); // true
popupsTestArray.push(popup);
console.log(popupsTestArray); // popup_isOpen is false...
});
我也遇到了同样的问题,并通过以下代码修复了它:
marker.on('click', function (e) {
if (e.target._popup == undefined) { // same as e.target.getPopup()
$.getJSON(url,
{ entityObject: e.target.options.alt },
function (infoBoxContent ) {
// e.target.options.alt contains entity Id
// from which we will get Infobox window content.
e.target.bindPopup(infoBoxContent).openPopup();
});
}
else {
marker.openPopup();
}
});
在
if
条件下,我们从服务器端获取数据并将其绑定到标记弹出窗口。
在其他情况下,再次单击相同的标记后,我们将显示来自客户端的内容。
这段代码对我来说工作得很好。如果您有任何疑问,我们可以在这里讨论。