【问题标题】:How to solve this problem with pop-ups in Leaflet如何通过 Leaflet 中的弹出窗口解决此问题
【发布时间】:2021-12-30 19:14:19
【问题描述】:

我有一个网站,上面有一张地图,上面有不同景点的标记。我正在使用下面完美运行的 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);
}

但是当我尝试使用下面的代码为每个标记添加一个弹出窗口时,整个地图向左移动,不再以 setView 定义的初始位置为中心:

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();
}

知道发生了什么以及解决问题的最佳方法吗? 提前谢谢你。

【问题讨论】:

    标签: javascript dictionary leaflet


    【解决方案1】:

    我找到了解决方案:问题在于弹出窗口是打开的。 使用以下代码行:

    marker.bindPopup("&lt;b&gt;mon titre"&lt;/b&gt;&lt;br&gt;mon texte").closePopup();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 2023-03-03
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多