简体   繁体   English

使用循环向我的传单地图添加图层

[英]adding layers to my Leaflet map using a loop

So here is one of my layers:所以这是我的图层之一:

               TurksAndCaicosLayer = L.geoJson(TurksAndCaicos, { 
                    style: {
                        weight:         0.5,
                        color:          'white',
                        fillOpacity:    1,
                        fillColor:      'brown',
                        }})         

I have 8 of these polygon layers for my Leaflet map.我的传单地图有 8 个这样的多边形图层。 I am trying to construct a loop which will go through the array of my layers and add them to the map, but it doesn't seem to be working.我正在尝试构建一个循环,它将遍历我的图层数组并将它们添加到地图中,但它似乎不起作用。 Can anyone spot why?任何人都可以发现为什么?

            let layers = [AnguillaLayer, BermudaLayer, BritishVirginIslandsLayer, GibraltarLayer, GuernseyLayer, IsleOfManLayer, JerseyLayer, TurksAndCaicosLayer]

                for (let layer of layers) {

                map.addLayer(layer)}

try尝试

layers.forEach(addLayer);

function addLayer(item, index) {
   map.addLayer(item);
}

An alternative approach would be to add your layers to a layerGroup and add the layerGroup to the map - saves writing an explicit loop.另一种方法是你的图层添加一个layerGroup和添加layerGroup的地图-保存写一个明确的循环。

let layers = [AnguillaLayer, BermudaLayer, BritishVirginIslandsLayer, GibraltarLayer, GuernseyLayer, IsleOfManLayer, JerseyLayer, TurksAndCaicosLayer];

let myLayerGroup = L.layerGroup(layers).addTo(map);

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

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