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);
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).
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);
var viz_layer = [1,2,3,5,6,7]; - Remove the GroupLayer ID
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 Codevar 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 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.
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 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/