我有一项功能,允许用户查找标记并缩放到其位置,此时我希望弹出窗口打开。一切正常,除了弹出功能触发后弹出窗口关闭。我无法弄清楚是什么让弹出窗口关闭。
这是问题的fiddle。如果我采用元素" .lookFor"并且位置在地图div之外,功能正常;但是我需要这个功能才能成为现实。它是Bootstrap标签式界面的一部分,用于保存其他地图工具,这些工具位于全屏地图上。
这是我的相关代码:
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!");
}
当警报触发时,您可以在地图上看到弹出窗口打开,但一旦警报关闭,弹出窗口就会消失。我确实有一个函数可以更新弹出窗口偏移量的位置以重置弹出窗口,如果之前已调整大小以适合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");
});
有没有人碰到类似的东西?
答案 0 :(得分:2)
我相信您的动画setView
正在关闭弹出窗口,作为moveend
事件的一部分。
相反,尝试使用此代码进行按钮单击。等待moveend
完成,然后准备弹出窗口。
jQuery("#lookForButton").click(function(){
map.on('moveend', function(e) {
zoomOpenPopup("11486990");
map.off('moveend');
});
map.setView([42.1676459, -121.776391], 14);
});