Layer control (TOC): Hide or Show layers and/or group layers

15226
21
09-02-2010 06:40 AM
AxelSchaefer
New Contributor II
Hi.

I don't know how to handle my layer control with the right JS API statements to the REST API.

I've got a very simple mapservice (called grouped) with a group layer and two sublayers:

World (0) (Grouplayer)
`-> World Country (1) (Layer)
`-> World Timezone (2) (Layer)


The JS-API Samples with the layer control makes use of the  ArcGISDynamicMapServiceLayer.setVisibleLayers() method to define the visible layers. The parameters for that method are the IDs of the layer.

That doesn't work for group-layers, like this layer "World" with the ID 0. If I write this command in the JS API:

layer.setVisibleLayers([0]);


I see all layers, even the unchecked layers 1 and 2. What happens, is that the REST interface gets an ExportMap request like this, when I only check the group layer 0 to active:

http://machine/ArcGIS/rest/services/grouped/MapServer/export?layers=show:0&...


The result is that all layers are shown: World Country and World Timezone, although they are not in the list of the visible layers!

The logic of checking and unchecking grouplayers works, if I use the HIDE method in the REST API. Hiding layer 0 (the grouplayer) returns an empty map. All combinations work with the HIDE option.

Question: How can I use the HIDE logic with the JS API? Where is the ArcGISDynamicMapServiceLayer.setHiddenLayers() method? 😉 How can I make a layerlist control with grouplayers?

Can I set the esri.layers.ImageParameters() at runtime after initialization? The ImageParameters have the layerOptions LAYER_OPTION_HIDE. But they are read out and used at initialization.

How can I achive the logic with the grouplayers?

Thanks in advance,
Axel
0 Kudos
21 Replies
mikesmith5
New Contributor
We have implemented the TOC layer control and are currently saving the state of the visible layers to a database.  We then retrieve the visible layers if a user presses a load button and update the map based on their saved view. 

We are using a dynamicmapservice and we pass the array to the setVisibleLayers of the service after the load.  This updates the map correctly, however, all of the checkboxes in the TOC remain checked.  Is there a preferred way of updating the TOC checkboxes?
0 Kudos
JeffPace
MVP Alum
We went about this slightly differently, showing layers based on scale dependencies, sort of like the old toclayer function in ArcIMS.

Our Site

It is a pretty customized version of the JavaScript sample viewer.
0 Kudos
AxelSchaefer
New Contributor II
Hi Axel,

Thanks again for your input. You have really helped me thus far.

I understand why we wouldn't have to show the feature layer,  but I'm actually selecting features by using a freehand tool, so the layer needs to be turned on in order for the selection to work. I'm currently using MODE_ONDEMAND to perform the select.

So, my problem is when I turn off the layer in the TOC, the layer is still turned on the feature layer). How do I control the feature layer in the TOC? Is it possible? I know the layer in the TOC is part of the basemap, as I created it in the grouped.json file.

In total, I'll probably have around 6 or so feature layers, so this is an important part of my application. The user needs to have the ability to turn the feature layers on and off, and be able to select the feature layers if they are turned on.

Thanks again,
Andrew


Hi Andrew,

sorry for the delay. I guess, you have to build the logic yourself. If the layer isn't visible you have to turn off the featurelayer. I haven't worked on an app like this, so far. So I don't know how to deal with feature-layers and how to "switch them off".

I guess you may implement 2 ways: The hook of the freehand selection has to ask if the layer is visible. Otherwise it won't select. The second logic is in the tree. If the layer-id of your featurelayer isn't visible, you are not able to select it. Depends on the way of your implementation.

On the other hand you have not to show the FeatureLayers on the map, if they are visible with the map-service calls. You don't have to show the symbols. You only need the results of your selection on the map (for example the blue/green highlight).

Depending on your workflow and application. Several ways to implement, but you have to implement the communication between the map-service layers and your feature-layers.

Axel
0 Kudos
AxelSchaefer
New Contributor II
We have implemented the TOC layer control and are currently saving the state of the visible layers to a database.  We then retrieve the visible layers if a user presses a load button and update the map based on their saved view. 

We are using a dynamicmapservice and we pass the array to the setVisibleLayers of the service after the load.  This updates the map correctly, however, all of the checkboxes in the TOC remain checked.  Is there a preferred way of updating the TOC checkboxes?


I don't know the preferred way of updating the checkboxes from outisde. Currently it works as the main control. Please check the documentation of the Widget: http://www.thejekels.com/ if it's possible. Since it's based on a DOJO tree, that API might also help. I guess there should be a way for doing this, but I haven't done it yet. 😞

Axel
0 Kudos
AxelSchaefer
New Contributor II
Just for information, since the discussion is always coming up and people may end up in this thread.

Another nice example of a TOC is here: http://www.arcgis.com/home/item.html?id=9c43bdf76a23452ba1d95684f7cd76d8 by nliu. It integrates the legend directly under the layer-control, works also with group-layers (although I don't know if you can switch them seperately), has a transparency control, a scale dependecy and other nice things.
0 Kudos
AndrewBrown1
Occasional Contributor II
Thanks, this is really great! I already incorporated it into our site... now I'm going to try and modify it a little bit to include group layer control.

Thanks again
0 Kudos
jamiehollingsworth
New Contributor II
very helpful thanks for posting

http://ltergis.iab.uaf.edu/nsn.html
0 Kudos
Reynaldde_Castro
Occasional Contributor
How would I be able to keep track which layers have been checked from the TOC?  This would be handy to pass this to the identify parameter.
0 Kudos
BarryGuidry
Occasional Contributor
axxl,

I changed the path in the variable worldgroupedUrl to point to my map service, which displays correctly (in addition to changing the map extent of course).

I have also changed the layers in the 'worldgrouped.json' file (below; mine has no groups though only layers). Does this look correct? I cannot figure out how to change the 'WorldGrouped' title in the TOC (tried changing it in the html file), nor does it display my layer in the TOC.

Please help.
{
 identifier: 'name',
 label: 'name',
 items:
 [
  {
   name : 'Gates',
   type : 'layer',
   id: 0,
   visible: true,
   children:
  },
  {
   name : 'Fences',
   type : 'layer',
   id: 1,
   visible: true
  },
  {
   name : 'Railways',
   type : 'layer',
   id: 2,
   visible: true
  },
  {
   name : 'Primary Roads',
   type : 'layer',
   id: 3,
   visible: true
  },
  {
   name : 'Roads - Other',
   type : 'layer',
   id: 4,
   visible: true
  },
  {
   name : 'SWMUs',
   type : 'layer',
   id: 5,
   visible: true
  },
  {
   name : 'SWMU Extents',
   type : 'layer',
   id: 6,
   visible: true
  },
  {
   name : 'Lakes',
   type : 'layer',
   id: 7,
   visible: true
  },
  {
   name : 'Buildings/Structures',
   type : 'layer',
   id: 8,
   visible: true
  },
  {
   name : 'Berms',
   type : 'layer',
   id: 9,
   visible: true
  },
  {
   name : 'Property Boundary',
   type : 'layer',
   id: 10,
   visible: true
  }
 ]
}
0 Kudos
AxelSchaefer
New Contributor II
Woo, it's been a long time... Check out also the nice example with legend symbols by nliu at: https://www.arcgis.com/home/item.html?id=9b6280a6bfb0430f8d1ebc969276b109 if you like.

The "WorldGrouped" label is part of the model. The rootLabel attribute. Consider my example also as a nice way dealing with stores, models and views.

    worldgroupedModel = new tmpdir.CheckBoxStoreModel({
[...]
        rootLabel: 'Your text here...',
[...]
    });


Also your JSON has a children attribute at the "Gates" layer. Delete that line and the comma behind the last attribute.

Axel
0 Kudos