I am adding several layers (both dynamic and tiled) programmatically to my map. In the layer-add-result event, I am setting the visibility to false and the visibleLayers to -1 for all the layers.
map.on("layer-add-result", function (addedLayer) { addedLayer.layer.setVisibility(false); addedLayer.layer.setVisibleLayers([-1]); });
However, when I create the map with a basemap, the basemap layer( or layers, since some basemaps have multiple layers) will also fire this event. Since I want to keep the basemap visible, how can I determine if the layer a basemap?
Solved! Go to Solution.
There may be a few ways to find that info:
In general, the base layer will be the very first layer (index = 0) in map.layerIds.
Thus, var layer = map.layerIds[0] by default will get you the basemap.
If you have more than one basemap, you can find an array of basemap with basemapLayerIds property.
So, var arrLayers = map.basemapLayerIds will do.
Another way to find the current basemap:
var tempMap = document.getElementById("map");
console.log(tempMap.dataset.basemap);
(Note: I didn't check my spelling on these, so they may not be exact.)
There may be a few ways to find that info:
In general, the base layer will be the very first layer (index = 0) in map.layerIds.
Thus, var layer = map.layerIds[0] by default will get you the basemap.
If you have more than one basemap, you can find an array of basemap with basemapLayerIds property.
So, var arrLayers = map.basemapLayerIds will do.
Another way to find the current basemap:
var tempMap = document.getElementById("map");
console.log(tempMap.dataset.basemap);
(Note: I didn't check my spelling on these, so they may not be exact.)
Thanks, I ended up using this code
map.on("layer-add-result", function (addedLayer) { var isBaseLayer = false; array.forEach(map.basemapLayerIds, function (baseLayer) { if (addedLayer.layer.id === baseLayer) { isBaseLayer = true; } }) if (isBaseLayer) { return; } addedLayer.layer.setVisibility(false); if (addedLayer.layer.declaredClass !== "esri.layers.ArcGISTiledMapServiceLayer") { addedLayer.layer.setVisibleLayers([-1]); } });
Or be lazy ..
if ( map.basemapLayerIds.indexOf(addLayer.layer.id) == -1) { // not baselayer
// do whatever
}
True, although it would be better to use the dojo/_base/array indexOf method if there are clients who are using IE 8 or older.
map.on("layer-add-result", function (addedLayer) { if (array.indexOf(map.basemapLayerIds, addedLayer.layer.id) === -1) { addedLayer.layer.setVisibility(false); if (addedLayer.layer.declaredClass !== "esri.layers.ArcGISTiledMapServiceLayer") { addedLayer.layer.setVisibleLayers([-1]); } } });
I thought this was going to be the answer, but it turns out that if I change the basemap using the BasemapGallery dijit, this won't work. I discovered that map.basemapLayerIds does not change when another basemap is selected, even though the map.layerIds does change.
Could you use the basemapGallery.getSelected method to get the currently selected basemap from the gallery and use that id to compare?
The basemap's id does not correspond to the map's layer ids of the basemap...if that makes sense. This JSBin will show that.
Thanks for the JSBin - I now see that the ids are different. What about if you add the layers in your app using map.addLayers instead of addLayer. Then you could listen for the layers-add-result event instead of layer-add-result. When the basemap is switched by the gallery it uses addLayer so the corresponding layer-add-result event is fired instead of layers-add-result.
map.on("layers-add-result", function(result){ console.log(result); console.log("Layers added"); }); map.addLayers([new ArcGISDynamicMapServiceLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer", { "id": "usa" })]);
The problem with map.basemapLayerIds has been deemed a bug (BUG-000083794).
What I ended up doing is to put in a Boolean so that this code only runs when the map is initialized.
map.on("layer-add-result", function (addedLayer) { if (initializing) { if (array.indexOf(map.basemapLayerIds, addedLayer.layer.id) === -1) { if (addedLayer.layer !== layerDynamic && addedLayer.layer !== layerResultsGraphic) { addedLayer.layer.setVisibility(false); if (addedLayer.layer.declaredClass !== "esri.layers.ArcGISTiledMapServiceLayer") { addedLayer.layer.setVisibleLayers([-1]); } } } } });