ArcGIS API for Javascript: "Reorder layers in map service" code example not working?

4975
3
03-30-2015 03:34 PM
AndrewWallick2
New Contributor III

I'm trying to use ESRI's Javascript API and when I view the live sample for "Reorder layers in map service" under "Dynamic Layers" I turn off all the check boxes. One by one they go off, but once the last one is off all the layers show up again on the map. Is this supposed to happen? It seems unintuitive.

If it is supposed to happen, anyone know how I can make it not happen in my own code?

Here's the link to the sample: Reorder layers in map service | ArcGIS API for JavaScript

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

Yes, there is a bug in the code that does that. In addition, if the Lakes layer is added and all the layers are turned off, you can't turn the Lakes layer on again. What's happening in the code is that the setVisibleLayers property is set to [] instead of [-1]. Using an empty array makes all the layers visible.

I was able to fix this by making this change to the toggleLayer function

function toggleLayer(e) {
    showLoading();
    for (var info in infos) {
        var i = infos[info];
        if (i.name === e.target.name) {
            i.visible = !i.visible;
        }
    }
    var visible = getVisibleLayers();
    if (visible.length === 0) {
        usaLayer.setVisibleLayers([-1]);
    } else {
        usaLayer.setDynamicLayerInfos(visible);
    }
}
AkshayHarshe
Esri Contributor

Hi Andrew,

Thanks for bringing that to our attention and thanks Ken for providing a workaround. I have submitted a bug against this and added suggested workaround. I hope this will get fixed soon.

Here is the bug# for your reference BUG-000086608 Reorder layers in map service sample works incorrectly when all layers are turned off.

Please find the attached sample.

Thanks

Akshay

Thanks,
Akshay Harshe
0 Kudos
HeatherGonzago
Esri Contributor

Hi Andrew,

We've updated the code snippet so that it should now work as you would expect. You can expect to see these updates with our next publication to our SDK.