简体   繁体   中英

Leaflet dynamically create marker layers

I'm new to leaflet and I'm stuck here. I am receiving an xml with locations and their type, (lat, long, type). I want to create a marker layer for every unique type that I get but It's not possible to know how many types I'll be getting each time. So I would like to ask if it is possible to make an array of layers, something like:

for (var i = 0; i < locTypes.length; i++){

    var markerLayers[i] = new L.layerGroup();
    //populateLayer();
}

My target is to hide/show specific marker layers on the map.

Well, I finally managed to make it work: locInfo is a 3d array which stores 1.Location type 2.Place 3.Lat and Long

let marker;
markerLayers = [];

//for every type
for (let i = 0; i < locInfo.length; i++) 
{
    markerLayers[i] = new L.layerGroup();

    //for every place of the specific type
    for (let j = 0; j < locInfo[i].length; j++)
    { 
        marker = new L.marker([locInfo[i][j][0],locInfo[i][j][1]]);
        markerLayers[i].addLayer(marker);
    }
    map.addLayer(markerLayers[i]);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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