Select to view content in your preferred language

Determining if a layer is a baselayer in layer-add-result event

2832
12
Jump to solution
11-26-2014 08:19 AM
KenBuja
MVP Esteemed Contributor

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?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
YungKaiChin
Regular Contributor

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.)

View solution in original post

0 Kudos
12 Replies
YungKaiChin
Regular Contributor

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.)

0 Kudos
KenBuja
MVP Esteemed Contributor

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]);
    }
});
0 Kudos
YungKaiChin
Regular Contributor

Or be lazy ..

if ( map.basemapLayerIds.indexOf(addLayer.layer.id) == -1) { // not baselayer

     // do whatever

}

KenBuja
MVP Esteemed Contributor

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]);
        }
    }
});
0 Kudos
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Could you use the basemapGallery.getSelected method to get the currently selected basemap from the gallery and use that id to compare?

0 Kudos
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
KellyHutchins
Esri Frequent Contributor

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"
            })]);
0 Kudos
KenBuja
MVP Esteemed Contributor

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]); }
            }
        }
    }
});
0 Kudos