Searching for Layers via indexOf()

Going through the documents, I was trying to see if I could search through the ImageryLayerCollection via indexOf() to find an imagery layer that I added, though I’m not sure what to pass in as the ‘layer’ parameter since there’s no layer name or variable in my code. I’m adding the layer like this:

var imageryLayers = viewer.scene.imageryLayers;
imageryLayers.addImageryProvider(new Cesium.WebMapServiceImageryProvider({
url : ‘http://nowcoast.noaa.gov:80/wms/com.esri.wms.Esrimap/wwa’,
layers : ‘NHC_TRACK_POLY,NHC_TRACK_LIN,NHC_TRACK_PT,NHC_TRACK_WWLIN,NHC_TRACK_PT_72DATE,NHC_TRACK_PT_120DATE,NHC_TRACK_PT_0NAMEDATE,NHC_TRACK_PT_MSLPLABELS,NHC_TRACK_PT_72WLBL,NHC_TRACK_PT_120WLBL,NHC_TRACK_PT_72CAT,NHC_TRACK_PT_120CAT’,
parameters : {
transparent : true,
format : ‘image/png’
}
}));

``

Though it’s easy enough to use get() by passing in the index number for the desired imagery object…

var wms = viewer.scene.globe.imageryLayers.get(1);
wms.show = false;

``

I was thinking that I might be able to pass in an id, layername, or a class similar to OpenLayers (ie. map.getLayersByName() or map.getLayersByClass() where it will bring back the layer object) but I’m not sure if I’m not looking in the right places. As my application gets larger and more complex, I’ll be less confident that the wms layer will always be at index 1.

Am I thinking about this process backwards?

Thanks,

Zach

The return value of addImageryProvider() is the newly created layer. Simply store that in a variable.

var wms = imageryLayers.addImageryProvider(new Cesium.WebMapServiceImageryProvider(…

Thanks for the quick reply.

I’m running into scoping issues when trying to access the variable wms.

Is there any other possible work around?

ImageryLayers don’t really have any identifying information except for index. You could loop over the layers and examine the imageryProvider property to see if it’s the layer you’re looking for.

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

var layer = imageryLayers.get(i);

var imageryProvider = layer.imageryProvider;

// then perhaps check imageryProvider instanceof Cesium.WebMapServiceImageryProvider

// or imageryProvider.url

}