如何在传单中批量添加标记?

2022-01-12 00:00:00 openstreetmap leaflet javascript

我有一个包含约 30k 个元素的数组,我需要为每个元素创建带有标记的地图.我使用markerclusters并尝试优化添加时刻.

I have an array with ~30k elements and I need to create map with markers for each of them. I use markerclusters and trying to optimize adding moment.

for (var i = 0; i < myItems.length; i++) {
    var item = myItems[i];

    marker = new L.marker([item[2],item[3]], {
        icon: mapOpts.myIcon
    }).bindPopup(item[1]);

    markers.addLayer(marker);

}

即使是谷歌浏览器也需要大约 40 秒来完成这个循环.我不想看到FF的结果.

Even Google Chrome takes about 40 sec to do this loop. I don't want to see FF's result.

有没有办法优化向地图添加许多元素?

Is there any way to optimize adding many elements to map?

推荐答案

var array = [];

for (var i = 0; i < myItems.length; i++) {
    var item = myItems[i];

    marker = new L.marker([item[2],item[3]], {
        icon: mapOpts.myIcon
    }).bindPopup(item[1]);

    array.push(marker);
}

markers.addLayers(array);

有关详细信息,请参阅 文档.

See the docs for more details.

相关文章