简体   繁体   English

如何通过 Leaflet 中的弹出窗口解决此问题

[英]How to solve this problem with pop-ups in Leaflet

I have a website featuring a map with a marker for different places of interest.我有一个网站,其中包含一个 map 和不同景点的标记。 I am using the js code below that works perfectly:我正在使用下面完美运行的 js 代码:

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);
        
//.  t is an array containing gps positions of the different places
for(i=0;i<t.length;i++){
    var marker = L.marker([t[i][lat], t[i][lon]]).addTo(mymap);
}

But when I try to add a pop-up for each marker with the code below, the whole map is moved to the left, and not centered anymore on the initial position defined with setView:但是当我尝试使用下面的代码为每个标记添加一个弹出窗口时,整个 map 被移到左侧,并且不再以 setView 定义的初始 position 为中心:

for(i=0;i<t.length;i++){
    var marker = L.marker([t[i][lat], t[i][lon]]).addTo(mymap);
    marker.bindPopup("<b>mon titre"</b><br>mon texte").openPopup();
}

Any idea of what is going on and the best way to solve the problem?知道发生了什么以及解决问题的最佳方法吗? Thank you in advance.先感谢您。

I found the solution: the problem comes from the fact that the pop-ups are open.我找到了解决方案:问题在于弹出窗口是打开的。 Using the following line of code works:使用以下代码行有效:

marker.bindPopup("<b>mon titre"</b><br>mon texte").closePopup();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM