<operationallayers> <layer label="gpsTrack" type="dynamic" visible="true" visiblelayers="1" url="http://176.34.114.255/ArcGIS/rest/services/fleetPoc/FleettracksJanLeen/MapServer"/> </operationallayers>
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(); }
Solved! Go to Solution.
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(); } }
somearraycollection.removeItemAt(item index);
var itemIndex:int = somearraycollection.getItemIndex(item);
somearraycollection.removeItemAt(itemIndex);
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(); } }
somearraycollection.removeItemAt(item index);
var itemIndex:int = somearraycollection.getItemIndex(item);
somearraycollection.removeItemAt(itemIndex);
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);
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(); } }
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(); } }
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(); }