Enabling layers

880
7
10-17-2011 11:16 PM
FaizanTayyab
Occasional Contributor
Hi,

i have tried to use the code below to make the layer visible.

map.getLayer("USA Daytime Population").visible=ture


but it is giving me a null reference error. Doesnt the getLayer method accept sub layers?

Is there anyway to make a certain sublayer visible based on a certain condition.

Thanks
Tags (2)
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus
Faizan,

   No getLayer is not for individual layers only the whole map service to enable visibility on a particular layer in the map service you would use visibleLayers array Collection. Look at the documentation for a layer like ArcGISDynamicMapService and you will see visible layers property and some examples of how to use it.
0 Kudos
FaizanTayyab
Occasional Contributor
Faizan,

   No getLayer is not for individual layers only the whole map service to enable visibility on a particular layer in the map service you would use visibleLayers array Collection. Look at the documentation for a layer like ArcGISDynamicMapService and you will see visible layers property and some examples of how to use it.


Thanks for the reply. In terms of visibleLayers, i do not get the group layers returned. is this a built in limitation?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Faizan,

   Yes the visibleLayers array collection will never list the id of a group layer but you can add that id to turn on the group.
0 Kudos
FaizanTayyab
Occasional Contributor
Faizan,

   Yes the visibleLayers array collection will never list the id of a group layer but you can add that id to turn on the group.


Thanks for the advice. I thought the visibleLayers was a read only property but found out that it is read and write. Although i got it work fine, i am unable to get the toc to reflect the same. I am assuming that requires more logic to update the toc with the visibility of each sublayer.
0 Kudos
FaizanTayyab
Occasional Contributor
I got some code from another thread which helped in enabling the toc based upon visibilty of the layer. It works great for child layers in a map service layer but doesnt work for the group layers containing further sub layers.

The result i get is that the unexpectedly all sublayers under the group layer become visible yet the tick boxes for those layers remain unchecked (which is fine), however i am not sure why the sublayers become visible for example

If i have a group layer

Infrastructure
----buildings
----structures
----roads

and i use the code below to only put tick boxes for the group layer called Infrastructure and buildings, it does work and only those checkboxes are ticked but i also get structures and roads visible on the map yet their checkboxes are unticked.

var visLayers:ArrayCollection = dmsl.visibleLayers;
var newVisLayer:ArrayCollection=new ArrayCollection(obj.visLyrs.visibleLayer);
for each(var j:int in newVisLayer){
AppEvent.dispatch(AppEvent.LAYER_VISIBILITY_CHANGED,j);
}


In toc Item i have

public function TocItem( parentItem:TocItem = null )
{
 _parent = parentItem;
 //ViewerContainer.addEventListener(AppEvent.LAYER_VISIBILITY_CHANGED, updateCB);
 AppEvent.addListener(AppEvent.LAYER_VISIBILITY_CHANGED,updateCB);
}

private function updateCB(event:AppEvent):void
{
 if(this is TocLayerInfoItem)
            {
           var tli:TocLayerInfoItem = this as TocLayerInfoItem;
    
  if(tli.layerInfo.id == event.data){
   setVisibleDirect(_visible ? false : true);
  }
 }
0 Kudos
FaizanTayyab
Occasional Contributor
I got some code from another thread which helped in enabling the toc based upon visibilty of the layer. It works great for child layers in a map service layer but doesnt work for the group layers containing further sub layers.

The result i get is that the unexpectedly all sublayers under the group layer become visible yet the tick boxes for those layers remain unchecked (which is fine), however i am not sure why the sublayers become visible for example

If i have a group layer

Infrastructure
----buildings
----structures
----roads

and i use the code below to only put tick boxes for the group layer called Infrastructure and buildings, it does work and only those checkboxes are ticked but i also get structures and roads visible on the map yet their checkboxes are unticked.

var visLayers:ArrayCollection = dmsl.visibleLayers;
var newVisLayer:ArrayCollection=new ArrayCollection(obj.visLyrs.visibleLayer);
for each(var j:int in newVisLayer){
AppEvent.dispatch(AppEvent.LAYER_VISIBILITY_CHANGED,j);
}


In toc Item i have

public function TocItem( parentItem:TocItem = null )
{
 _parent = parentItem;
 //ViewerContainer.addEventListener(AppEvent.LAYER_VISIBILITY_CHANGED, updateCB);
 AppEvent.addListener(AppEvent.LAYER_VISIBILITY_CHANGED,updateCB);
}

private function updateCB(event:AppEvent):void
{
 if(this is TocLayerInfoItem)
            {
           var tli:TocLayerInfoItem = this as TocLayerInfoItem;
    
  if(tli.layerInfo.id == event.data){
   setVisibleDirect(_visible ? false : true);
  }
 }


I have got the group layers problem sorted however now i have another problem.

The function updateCB is repeatedly called for all map services. For example if i only want to loop through the sub layers of map service #1, it goes them fine but also goes through sub layers of map service #2.

How can i restrict it to only loop through 1 map service.

Any ideas
0 Kudos
deleted-user-0W0-oLHxDjCX
New Contributor III

Try evt.stopPropagation();

0 Kudos