简体   繁体   中英

How can I distinguish two different vector layers in OpenLayers?

In my design there's a checkbox which removes all vector layers with the following code.

// When I check that checkbox:
var layers = map.getLayersByClass('OpenLayers.Layer.Vector');

for(var j=0; j < layer_vectors.length; j++){
    map.removeLayer(layer_vectors[j]);
}

Which works fine. I need to remove all with this way because there're many vector layers which contain polygons.

What if I have two different group of vector layers? How can I remove only one of them by that checkbox?

Can we set something like a "data-layertype" attribute as in normal html elements? How can I distinguish two different layers after I get them with getLayersByClass?

Thanks

OK I solved this problem. I set whole group with the same name each time when I create the vector layer and when I remove the layers I distinguish them with their ".name" attribute.

for(var j=0; j < layer_vectors.length; j++){
    if(layer_vectors[j].name == "group_name"){
        map.removeLayer(layer_vectors[j]);
    }
}

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