[英]Automatically open popup on layer add
I have a polyline that needs to have a popup automatically open when the user selects the polyline layer in the layers control menu. 我有一条折线,当用户在图层控制菜单中选择折线图层时,需要自动打开弹出窗口。
This method did not work for me:
此方法对我不起作用:
var polyline = L.geoJson(myData).bindPopup("<h1>Some Text goes here</h1>").openPopup();
How can I do the auto popup? 我该如何自动弹出?
Here's my current setup: 这是我当前的设置:
var polyline = L.geoJson(myData).bindPopup("<h1>Some Text goes here</h1>");
var overlaysMenuCtrl = L.Control.extend({ ... blah blah... });
map.addControl(new overlaysMenuCtrl)());
I extend the control and add a custom function to check for layers that are toggled on/off by the user: 我扩展了控件并添加了一个自定义函数来检查用户打开/关闭的图层:
function toggleLayer(checked, layer){
if(checked){
map.addLayer(layer);
} else {
map.removeLayer(layer);
}
}
$(".check").change(function(){
var layerClicked = $(this).attr("id");
//Turn layers on and off based on the ID of the radio checked
switch(layerClicked){
case "polyline": toggleLayer(this.checked, polyline);
break;
...and other layers ...
}
});
HTML: HTML:
<label><input id="polyline" type="checkbox" class="check">Polyline layer</label>
There is very probably a delay between the moment you request to add your layer to the map, and when that layer is actually on map / ready to open its popup. 从您请求将图层添加到地图的那一刻到该图层实际在地图上/准备好打开其弹出窗口之间的时间之间可能会有一个延迟。
You could simply add an event listener (use the "add"
event) that will automatically open the popup whenever the layer is added to the map: 您可以简单地添加一个事件侦听器(使用
"add"
事件),只要将图层添加到地图,该侦听器就会自动打开弹出窗口:
layer.on("add", function (event) {
event.target.openPopup();
});
Demo: https://jsfiddle.net/3v7hd2vx/101/ (use the Layers Control to add the layer to the map) 演示: https : //jsfiddle.net/3v7hd2vx/101/ (使用“图层控件”将图层添加到地图)
将折线添加到地图后 ,运行openPopup()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.