visibleLayers and setVisibleLayers

7222
16
04-26-2011 01:05 PM
HeathClark
New Contributor III
So I started playing with a simple custom legend by trying the following code against a DynamicLayer:

  var vL = df_layer_data.visibleLayers;
  var layer_id_index = dojo.indexOf(vL, 2);

                if (layer_id_index != -1)
                    vL.splice(layer_id_index, 0);
                else
                    vL.push(2);

  df_layer_data.setVisibleLayers(vL);


I fully expected to see Layer #2 (a pipeline layer in this case) toggle visibility.  However, it didn't.  After a few minutes of "hacking", I found the problem, the service is a compilation of grouped feature classes and the setVisibleLayers call doesn't like having the ID's for the group layers in the array.

If setVisibleLayers (and maybe other functions) doesn't like and can't handle group layers, why does visibleLayers return them?  There might just be a great answer to that and would really like to hear it so I can learn something new today (or whenever).
0 Kudos
16 Replies
HemingZhu
Occasional Contributor III
So I started playing with a simple custom legend by trying the following code against a DynamicLayer:

  var vL = df_layer_data.visibleLayers;
  var layer_id_index = dojo.indexOf(vL, 2);

                if (layer_id_index != -1)
                    vL.splice(layer_id_index, 0);
                else
                    vL.push(2);

  df_layer_data.setVisibleLayers(vL);


I fully expected to see Layer #2 (a pipeline layer in this case) toggle visibility.  However, it didn't.  After a few minutes of "hacking", I found the problem, the service is a compilation of grouped feature classes and the setVisibleLayers call doesn't like having the ID's for the group layers in the array.

If setVisibleLayers (and maybe other functions) doesn't like and can't handle group layers, why does visibleLayers return them?  There might just be a great answer to that and would really like to hear it so I can learn something new today (or whenever).


You are on the right track. Just be aware of a couple of things. Suppose you have a parent layer 0, and a couple of sublayer 1 and 2. If you set the parent layer 0 visible, you still have to set the visibility of layer 1 and 2 for them to show or not show - these visible layer array [0,1] or [0,2] or [0,1,2] are legal, while those - [1] or [2] or [1,2] are illegal. If you set parent layer 0 not visible, then neither 1 nor 2 should in visible layer array. You can use LayerInfo's parentLayerId and subLayerIds properties to manipulate the situation.
0 Kudos
HeathClark
New Contributor III
From my testing I have found different results than what your post would suggest, for example:

Sample MapService

Facility Layer(0)    - Group Layer
Building(1)    - Feature Class
Pipeline(2)    - Feature Class
Encasement(3)    - Feature Class
Another Facility Layer(4)  - Group Layer
Vaults(5)    - Feature Class
Fittings(6)    - Feature Class
Easements(7)    - Feature Class

All within one dynamic map service.

Scenario One:
var vL = [0,1,2,3,4,5,6,7];    -Visible Layer Array
var layer_id_index = dojo.indexOf(vL, 2);                
if (layer_id_index != -1)
vL.splice(layer_id_index, 1);
               
dynamic_layer_data.setVisibleLayers(vL);



Results:
No change in visible layers.

Modification to Code
var viz_layer = [1,2,3,5,6,7];  - Remove the GroupLayer ID


Results:
Pipeline Layer is �??Hidden�?�.

I have found that including the GroupLayer ID�??s in the visible layer array causes the setVisibleLayers method to fail (ie. [0,1,2] is a not-valid/illegal and [1,2] is a valid/legal array)


Scenario Two:
If the dynamic_layer_data contains a large list of layers (I have tested against 633 with divided across 75 group layers), the same code above under the modified scenario does not work.  I could painstaking try to find the �??cut-off�?� point but would like to know if there is a definitive (ESRI) answer on if the setVisibleLayers method works on an infinite number of layers or if it can only handle 50, 100, etc.
0 Kudos
HemingZhu
Occasional Contributor III
From my testing I have found different results than what your post would suggest, for example:

Sample MapService

Facility Layer(0)    - Group Layer
Building(1)    - Feature Class
Pipeline(2)    - Feature Class
Encasement(3)    - Feature Class
Another Facility Layer(4)  - Group Layer
Vaults(5)    - Feature Class
Fittings(6)    - Feature Class
Easements(7)    - Feature Class

All within one dynamic map service.

Scenario One:
var vL = [0,1,2,3,4,5,6,7];    -Visible Layer Array
var layer_id_index = dojo.indexOf(vL, 2);                
if (layer_id_index != -1)
vL.splice(layer_id_index, 1);
               
dynamic_layer_data.setVisibleLayers(vL);



Results:
No change in visible layers.

Modification to Code
var viz_layer = [1,2,3,5,6,7];  - Remove the GroupLayer ID


Results:
Pipeline Layer is �??Hidden�?�.

I have found that including the GroupLayer ID�??s in the visible layer array causes the setVisibleLayers method to fail (ie. [0,1,2] is a not-valid/illegal and [1,2] is a valid/legal array)


Scenario Two:
If the dynamic_layer_data contains a large list of layers (I have tested against 633 with divided across 75 group layers), the same code above under the modified scenario does not work.  I could painstaking try to find the �??cut-off�?� point but would like to know if there is a definitive (ESRI) answer on if the setVisibleLayers method works on an infinite number of layers or if it can only handle 50, 100, etc.


I went back looking at my code and my notes. i have done it in silverlight/wpf api which is almost identical to js api in terms of visibility properties and methods. What i said here is exactly what my notes said. So I tested this issue using JS api and an esri map services: http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer. You are largely right on this expect I found out that whehter putting group layer id in visible id array or not doesn't make any different (visible = [0, 1, 3, 4, 5] vs visible = [0, 1, 2, 3, 4, 5] -2 is the group layer). I am very interested in ESRI or someone's input on this.
0 Kudos
HeathClark
New Contributor III
I am still not getting the same results with the group_layer_id issue, however, the bigger challenge right now is the fact that the setVisibleLayers method crashes the service with a large number of layers in the array (500+ for a large area map that is our production service representing 80 facilitieis across 6 counties).  It creates a cache image of the current screen when I toggle a layer, then if I zoom-out / pan, all I get is that cached image of the dynamic service with no-redraw / refresh.  All other services respond correctly.

I agree, some insight/input from ESRI staff would be great on both issues.
0 Kudos
HemingZhu
Occasional Contributor III
I am still not getting the same results with the group_layer_id issue, however, the bigger challenge right now is the fact that the setVisibleLayers method crashes the service with a large number of layers in the array (500+ for a large area map that is our production service representing 80 facilitieis across 6 counties).  It creates a cache image of the current screen when I toggle a layer, then if I zoom-out / pan, all I get is that cached image of the dynamic service with no-redraw / refresh.  All other services respond correctly.

I agree, some insight/input from ESRI staff would be great on both issues.


One thing i remembered from reading the document (David Peter's "Building a GIS: System Architecture Design Strategies for Managers") is that avoid to put too many layers in a one map services and tried to split layers into several map services.
0 Kudos
by Anonymous User
Not applicable
I had some time today and just wanted to comment.  Hopefully the following will help resolve any unsettled issues regarding visible layers and how to control them.

I did some fact checking and as expected visibleLayers returns an array of visible layer index values and does not return group layer index values.  See my screen shots.

The key way to dynamically manipulate what's visible is by adding or subtracting layer index values in an array which gets passed into setVisibleLayers().  This can be done by using native splice and push JavaScript methods.  See this Esri sample, specifically the toggle function.

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/widget_legendv...


As for a limit to the number of layers which can be passed into setVisibleLayers, I am not aware of any.  Hopefully this helps.


Regards,
Doug Carroll, ESRI Support Services SDK Team, Technical Lead
http://support.esri.com/
0 Kudos
HeathClark
New Contributor III
Interesting concept, except I don't want to try to manage multiple MXD/MSD files or lump things together, the organization we have makes sense within the facilities we manage.
0 Kudos
HemingZhu
Occasional Contributor III
Interesting concept, except I don't want to try to manage multiple MXD/MSD files or lump things together, the organization we have makes sense within the facilities we manage.


I totally understand. What David Peters said is from the performance point of view in term of system design and network.
0 Kudos
HemingZhu
Occasional Contributor III
I had some time today and just wanted to comment.  Hopefully the following will help resolve any unsettled issues regarding visible layers and how to control them.

I did some fact checking and as expected visibleLayers returns an array of visible layer index values and does not return group layer index values.  See my screen shots.

The key way to dynamically manipulate what's visible is by adding or subtracting layer index values in an array which gets passed into setVisibleLayers().  This can be done by using native splice and push JavaScript methods.  See this Esri sample, specifically the toggle function.

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/widget_legendv...


As for a limit to the number of layers which can be passed into setVisibleLayers, I am not aware of any.  Hopefully this helps.


Regards,
Doug Carroll, ESRI Support Services SDK Team, Technical Lead
http://support.esri.com/


Doug, I respect what you said, but that is not what i got when i test on http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/. It cleared show that layer.visibleLayers =[0,1,2,3,4,5]. among them 2 is a group layer. Do i missed something?
0 Kudos