Hide Tiled Layer Using Array

1772
5
Jump to solution
01-20-2016 11:22 AM
LauraMiles1
Occasional Contributor III

Hi all, I'm not sure where I'm going wrong. I have many many ArcGISTiledMapServiceLayers in my map that need to be hidden/shown en masse. So I created an array containing the names of each layer, and would like to use a loop to go through the array and hide each element.

var swipeLayerOrthos2015 = new ArcGISTiledMapServiceLayer("http://cop-gis4:6080/arcgis/rest/services/Orthos/Orthos2015_WGS1984_MIXED/MapServer");
        var swipeLayerOrthos2014 = new ArcGISTiledMapServiceLayer("http://cop-gis4:6080/arcgis/rest/services/Orthos/Orthos2014_WGS1984_MIXED/MapServer");
        var swipeLayerOrthos2014RB = new ArcGISTiledMapServiceLayer("http://cop-gis4:6080/arcgis/rest/services/Orthos/Orthos2014RB_WGS1984_MIXED/MapServer");

var orthoArray = [];

orthoArray.push('swipeLayerOrthos2015', 'swipeLayerOrthos2014', 'swipeLayerOrthos2014RB');

updateOrthos();

function updateOrthos(){       
        var targetOrtho;
       
        for (var i = 0; i < orthoArray.length; i++) {
            targetOrtho = orthoArray;
            targetOrtho.hide();
         }
    }

When I run this, I get an 'Uncaught TypeError: targetOrtho.hide is not a function'

I'm not sure what I'm doing wrong, if I change things so that a variable holds the ortho's name and then hide that, it works ie.

var targetOrtho = swipeLayerOrthos2010;

targetOrtho.hide();

Works just fine. I'm not sure how my loop is really different from that. I've tried printing out each item in orthoArray to the console and it looks ok to me. Does anyone see what I'm missing? I can make a jsfiddle if the above isn't clear.

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

This is because you're pushing strings into your array, and not your layers as desired.  Instead of

orthoArray.push('swipeLayerOrthos2015', 'swipeLayerOrthos2014', 'swipeLayerOrthos2014RB'); 

you should use

orthoArray.push(swipeLayerOrthos2015, swipeLayerOrthos2014, swipeLayerOrthos2014RB); 

View solution in original post

5 Replies
JimmyKroon
Occasional Contributor II

You've declared targetOrtho, but assigned no value to it because the loop is not setup to cycle through layers listed in orthoArray.

You need a loop that points targetOrtho to the values in orthoArray - something like this. (warning: I do python more than vb).

for targetOrtho in orthoArray:

  ... targetOrtho.hide

0 Kudos
LauraMiles1
Occasional Contributor III

Hi Jimmy,

targetOrtho = orthoArray;

Assigns the value to targetOrtho

0 Kudos
JimmyKroon
Occasional Contributor II

Yes. I guess my eyes missed that.

0 Kudos
JoelBennett
MVP Regular Contributor

This is because you're pushing strings into your array, and not your layers as desired.  Instead of

orthoArray.push('swipeLayerOrthos2015', 'swipeLayerOrthos2014', 'swipeLayerOrthos2014RB'); 

you should use

orthoArray.push(swipeLayerOrthos2015, swipeLayerOrthos2014, swipeLayerOrthos2014RB); 

LauraMiles1
Occasional Contributor III

Joel thank you, that was it!

0 Kudos