javascript - 为什么我的 Leaflet 弹出窗口意外关闭?

标签 javascript jquery popup leaflet

我有一项功能允许用户查找标记并缩放到其位置,此时我希望弹出窗口打开。一切正常,除了弹出窗口在函数触发后关闭。我不知道是什么让弹出窗口关闭。

这是一个 fiddle的问题。如果我采用元素“.lookFor”并且位置在 map div 之外,则该功能正常工作;但是我需要该功能位于它所在的位置。它是 Bootstrap 的一部分包含其他 map 工具的选项卡式界面将位于全屏 map 上。

这是我的相关代码:

var geoJsonDataFeatures = {
"type": "FeatureCollection",
"features":[]
};

//create popup content
var htmlString = "<div>popup html goes here....</div>";
var popupContent = L.popup().setContent(htmlString);

//create geoJson object for each marker
var geoJsonData = {
"type": "Feature",
"id": siteNo, //a string saved as a variable 
"properties": {
    "popupContent": popupContent,
    "icon": icon //an L.icon object
    },
"geometry": {
    "type": "Point",
    "coordinates": [longitude, latitude] //strings saved as variables
    }
}

//push the geoJson feature into the features object
geoJsonDataFeatures.features.push(geoJsonData);

//create the map layer that holds the markers
var geoJsonDataLayer = L.geoJson(geoJsonDataFeatures, {
    onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.popupContent);
    layer.setIcon(feature.properties.icon);
    }
}).addTo(map);

....

//when the user has selected a site from a dropdown select menu,
//we pass the site number (siteNo) to a function to zoom and open the
//marker popup:

zoomOpenPopup(siteNo);

function zoomOpenPopup(siteNo){
var marker = {};
var popup = {};
var layerObj = {};

    if(map.hasLayer(geoJsonDataLayer))
    {
    layerObj = geoJsonDataLayer;
    }

    jQuery.each(layerObj._layers, function(k){

        if(siteNo == layerObj._layers[k].feature.id)
        {
        marker = layerObj._layers[k];
        popup = layerObj._layers[k].feature.properties.popupContent;
        }  
    });

marker.openPopup();
//popup._isOpen = true; //doesn't work
//popup.openOn(map); //doesn't work
//map.openPopup(popup); //doesn't work
 alert("done!"); 
}

警报触发时,您可以在 map 上看到弹出窗口打开,但一旦关闭警报,弹出窗口就会消失。我确实有一个功能可以更新打开时弹出窗口偏移的位置以重置弹出窗口,如果它之前已调整大小以适合 Bootstrap 选项卡的内容,但即使我注释掉该功能,我也有弹出窗口关闭的相同问题并且重新加载页面。这是函数:

//when a popup opens, set the default offset
map.on("popupopen",function(e){
e.popup.options.offset = L.point(0,-13);
e.popup._updatePosition();
jQuery(".leaflet-popup-content").css("width","301px");
});

有人遇到过类似的事情吗?

最佳答案

我相信您的动画 setView 正在关闭弹出窗口作为 moveend 事件的一部分。

相反,请尝试使用此代码来单击按钮。它会等到 moveend 完全完成,然后准备弹出窗口。

jQuery("#lookForButton").click(function(){
    map.on('moveend', function(e) {
        zoomOpenPopup("11486990");
        map.off('moveend');
    });
    map.setView([42.1676459, -121.776391], 14);
});

关于javascript - 为什么我的 Leaflet 弹出窗口意外关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083320/

相关文章:

javascript - 在 jQuery $.each 中访问 JSON 中的多个对象/数组

jquery - DataTable 如何添加带有属性的行 Jquery?

popup - StackLayout isVisible 属性在 Xamarin Forms 中不起作用

javascript - jquery访问没有类名的表单

javascript - 用户信息作为前端的全局变量

javascript - jsHint - webpack 警告错误选项 : 'force' . @ line 0 char 0

Jquery将内容加载到div中

Javascript 点击多个链接

javascript - 如何使用 javascript 动态添加属性

javascript - 将计算值从弹出菜单传输到主窗体