Select to view content in your preferred language

little question: property 'visiblelayers' not recognized?

2523
4
Jump to solution
04-09-2012 11:38 AM
LeenD_hondt
Occasional Contributor
Hi,

I am trying to have make an application where the user can turn on/off the dynamiclayer of a mapservice. A very small question regarding this.

In my config file I have set 1 of the 2 layers visible using the visiblelayers property:
        <operationallayers>          <layer label="gpsTrack" type="dynamic" visible="true" visiblelayers="1" url="http://176.34.114.255/ArcGIS/rest/services/fleetPoc/FleettracksJanLeen/MapServer"/>          </operationallayers>


Then, in my mxml of my widget, I want that the user, once he checks an option of a dropdownlist, he can see that layer visible. There are only two layers. At startup, I put "1" visible (as mentioned above), but then the user should be able to make the second one visible (and the first one invisible) and vice versa. I do not think it can be very difficult, but the issue is that in my mxml, he does not recognize the property 'visiblelayers'.

protected function myDDL_changeHandler(event:IndexChangeEvent):void    {     var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;     var visLayers:ArrayCollection = dyn.visibleLayers;      if (myDDL.selectedItem.subject == "Track")      {       visLayers.removeItemAt(2);      visLayers.addItem(1);      dyn.visibleLayers=visLayers;      dyn.refresh();           }      else if (myDDL.selectedItem.subject == "Elevation")      {       visLayers.removeItemAt(1);      visLayers.addItem(2);      dyn.visibleLayers=visLayers;      dyn.refresh();           }  


However, the compiler gives an error: "RangeError: Index '1' specified is out of bounds."
But my service has the following layers:
    VehicleTracks (0)
        Tracks (1)
        TracksElevation (2)


Any tips to resolve this issue?

Thanks a lot in advance!
Kindly,
Leen
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IvanBespalov
Frequent Contributor
Leen,

Here is about 100 sublayers - in this sample i play with legend, but you can find some layer switching visibility code 2. (sources)


protected function myDDL_changeHandler(event:IndexChangeEvent):void {  var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;  // var visLayers:ArrayCollection = dyn.visibleLayers;   if (myDDL.selectedItem.subject == "Track")   {    // visLayers.removeItemAt(2);                 var visLayers:ArrayCollection = new ArrayCollection();                 visLayers.addItem(1);    dyn.visibleLayers = visLayers;   dyn.refresh();    }   else if (myDDL.selectedItem.subject == "Elevation")   {    // visLayers.removeItemAt(1);                 var visLayers:ArrayCollection = new ArrayCollection();   visLayers.addItem(2);    dyn.visibleLayers=visLayers;   dyn.refresh();     } }


you can't remove item at 100, if your collection length is 50!
somearraycollection.removeItemAt(item index);

to check item exists you need to get its index:
var itemIndex:int = somearraycollection.getItemIndex(item);

so, if itemIndex not equals "-1" means what item exists, you can remove existing item:
somearraycollection.removeItemAt(itemIndex);

View solution in original post

0 Kudos
4 Replies
IvanBespalov
Frequent Contributor
Leen,

Here is about 100 sublayers - in this sample i play with legend, but you can find some layer switching visibility code 2. (sources)


protected function myDDL_changeHandler(event:IndexChangeEvent):void {  var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;  // var visLayers:ArrayCollection = dyn.visibleLayers;   if (myDDL.selectedItem.subject == "Track")   {    // visLayers.removeItemAt(2);                 var visLayers:ArrayCollection = new ArrayCollection();                 visLayers.addItem(1);    dyn.visibleLayers = visLayers;   dyn.refresh();    }   else if (myDDL.selectedItem.subject == "Elevation")   {    // visLayers.removeItemAt(1);                 var visLayers:ArrayCollection = new ArrayCollection();   visLayers.addItem(2);    dyn.visibleLayers=visLayers;   dyn.refresh();     } }


you can't remove item at 100, if your collection length is 50!
somearraycollection.removeItemAt(item index);

to check item exists you need to get its index:
var itemIndex:int = somearraycollection.getItemIndex(item);

so, if itemIndex not equals "-1" means what item exists, you can remove existing item:
somearraycollection.removeItemAt(itemIndex);
0 Kudos
LeenD_hondt
Occasional Contributor
Thank you soooo much! It works!!

(can you just explain why? why can't the arraycollection be created at front and be updated within the if.. else.. loop?)
Thanks!

Leen, 

Here is about 100 sublayers - in this   sample i play with legend, but you can find some layer switching visibility code 2. (  sources


protected function myDDL_changeHandler(event:IndexChangeEvent):void
{
 var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;
 // var visLayers:ArrayCollection = dyn.visibleLayers; 
 if (myDDL.selectedItem.subject == "Track") 
 { 
  // visLayers.removeItemAt(2);
                var visLayers:ArrayCollection = new ArrayCollection();
                visLayers.addItem(1);

  dyn.visibleLayers = visLayers;
  dyn.refresh();  
 } 
 else if (myDDL.selectedItem.subject == "Elevation") 
 { 
  // visLayers.removeItemAt(1);
                var visLayers:ArrayCollection = new ArrayCollection();
  visLayers.addItem(2);

  dyn.visibleLayers=visLayers;
  dyn.refresh();   
 }
}


you can't remove item at 100, if your collection length is 50! 
somearraycollection.removeItemAt(item index);

to check   item exists you need to get its index: 
var itemIndex:int = somearraycollection.getItemIndex(item);

so, if   itemIndex not equals "-1" means what item exists, you can remove existing item: 
somearraycollection.removeItemAt(itemIndex);
0 Kudos
IvanBespalov
Frequent Contributor
Yes, ArrayCollection can be created at front and be updated within the if.. else.. loop...
protected function myDDL_changeHandler(event:IndexChangeEvent):void
{
    var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;
    var visLayers:ArrayCollection = dyn.visibleLayers;
    if (myDDL.selectedItem.subject == "Track") 
    { 
        // remove item = 2
        var indexOfItemToRemove:int = visLayers.getItemIndex(2);
        if (indexOfItemToRemove != -1)
        {
             visLayers.removeItemAt(indexOfItemToRemove);
        }
        // add item = 1
        visLayers.addItem(1); 
        dyn.visibleLayers = visLayers;
        dyn.refresh(); 
    } 
    else if (myDDL.selectedItem.subject == "Elevation") 
    {
        // remove item = 1
        var indexOfItemToRemove:int = visLayers.getItemIndex(1);
        if (indexOfItemToRemove != -1)
        {
             visLayers.removeItemAt(indexOfItemToRemove);
        }
        // add item = 2
        visLayers.addItem(2); 
        dyn.visibleLayers = visLayers;
        dyn.refresh(); 
    }   
}


1 more time, Yes, ArrayCollection can be created at front and be updated within the if.. else.. loop..., but can you answer me now, why is it needed (in your situation)?
protected function myDDL_changeHandler(event:IndexChangeEvent):void
{
    var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;
    if (myDDL.selectedItem.subject == "Track") 
    { 
        dyn.visibleLayers = new ArrayCollection([1]); // set collection of visible layers
        dyn.refresh();
    } 
    else if (myDDL.selectedItem.subject == "Elevation") 
    {
        dyn.visibleLayers = new ArrayCollection([2]); // set collection of visible layers
        dyn.refresh();
    }
}
0 Kudos
LeenD_hondt
Occasional Contributor
Hi, thanks. Well, the reason why I wanted to know is because I got a warning when I used your former code. On the second statement of the var vislayers it informed me:
"Duplicate variable definition." And next to that, I try to clean up my code as much as possible.

Thanks again! (your code with the legend is also very helpfull!)

Yes, ArrayCollection can be created at front and be updated within the if.. else.. loop... 
protected function myDDL_changeHandler(event:IndexChangeEvent):void
{
    var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;
    var visLayers:ArrayCollection = dyn.visibleLayers;
    if (myDDL.selectedItem.subject == "Track") 
    { 
        // remove item = 2
        var indexOfItemToRemove:int = visLayers.getItemIndex(2);
        if (indexOfItemToRemove != -1)
        {
             visLayers.removeItemAt(indexOfItemToRemove);
        }
        // add item = 1
        visLayers.addItem(1);  
    } 
    else if (myDDL.selectedItem.subject == "Elevation") 
    {
        // remove item = 1
        var indexOfItemToRemove:int = visLayers.getItemIndex(1);
        if (indexOfItemToRemove != -1)
        {
             visLayers.removeItemAt(indexOfItemToRemove);
        }
        // add item = 2
        visLayers.addItem(2);  
    }
    
    dyn.visibleLayers = visLayers;
    dyn.refresh();
}


1 more time, Yes, ArrayCollection can be created at front and be updated within the if.. else.. loop..., but can you answer me now, why is it needed (in your situation)? 
protected function myDDL_changeHandler(event:IndexChangeEvent):void
{
    var dyn:ArcGISDynamicMapServiceLayer= map.getLayer("gpsTrack") as ArcGISDynamicMapServiceLayer;
    var visLayers:ArrayCollection = new ArrayCollection();
    if (myDDL.selectedItem.subject == "Track") 
    { 
        visLayers.addItem(1); 
    } 
    else if (myDDL.selectedItem.subject == "Elevation") 
    {
       visLayers.addItem(2);
    }
    
    dyn.visibleLayers = visLayers;
    dyn.refresh();
}
0 Kudos