Select to view content in your preferred language

Dynamically Add/Remove Layers on Widget Open/Close

2309
6
03-26-2012 10:48 AM
KeithGerhartz1
Occasional Contributor II
I am creating a modified Edit widget allows one to specify which layers will be added to the map and be made available for editing.

I want to dynamically add the referenced layers when the widget is opened and remove them when it is closed.

The code below works but when I reopen the widget the layers are not added to the map which makes sense as that is done in init(). However, moving it to the widgetOpenedHandler causes the layers to be redundantly added.

I see a couple of threads regarding "really closing" widgets.

Any assistance with this would be greatly appreciated.

            [Bindable]
            private var featureLayersToAdd:Array = new Array;
            [Bindable]
            private var layerArray:Array = new Array;

            private function init():void
            {
                if (configXML) // checking for valid content in the configuration file
                {   
                    var lyrList:XMLList = configXML..layer;               
                    var URL:String;
                    var name:String;
                    var layerid:String;   
                    for (var i:Number = 0; i < lyrList.length(); i++){
                        URL = lyrList.@url;
                        name = lyrList.@name;
                        layerid = lyrList.@name;
                        layerArray.push({iLayerName:name,iLayerURL:URL,iLayerID:layerid});
                    }               
                    for (i = 0; i < layerArray.length; i++)
                    {
                        var layerToAdd:FeatureLayer = new FeatureLayer;
                        layerToAdd.url = layerArray.iLayerURL;
                        layerToAdd.name = layerArray.iLayerName;
                        layerToAdd.id = layerArray.iLayerID;
                        map.addLayer(layerToAdd);
                        featureLayersToAdd.push(layerToAdd);
                    }   
                    editor.featureLayers = featureLayersToAdd;   
                }               
            }
                       
            private function widgetClosedHandler(event:Event):void
            {
                for (var i:Number = 0; i < layerArray.length; i++)
                {
                    var layerToRemove:Layer = new Layer;
                    layerToRemove = map.getLayer(layerArray.iLayerID);
                    map.removeLayer(layerToRemove);
                }   
                editor.featureLayers = [];
            }
Tags (2)
0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus
Keith,

   I would suggest that you leave the code for adding the layer in the widget open handler but adding code to check if the layer is already present and if so don't add it.

                var editLayer:FeatureLayer = map.getLayer("youreditlayersid");
                if (editLayer is null)
                    //Add it code



Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:
0 Kudos
KeithGerhartz1
Occasional Contributor II
Robert,

True, and then I could just turn the layer on and off. However, the feature layer is added to the TOC when it is added to the map and I was wanting to remove it when the widget is closed to avoid redundancy and confusion. Should be a way to do this. I will keep looking and post the answer after I figure it out ... unless, of course, one of you other fine FLEXers comes to my rescue first :-).
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Keith,    You would still remove the layer when the widget is closed.
0 Kudos
RyanColeman
Occasional Contributor
I'm using this to open an annexations history layer with the TimeWidget but it's not working right

            [Bindable]
            private var annexLayer:FeatureLayer = map.getLayer("Annexations");


gives me the error: Implicit coercion of a value with static type com.esri.ags.layers:Layer to a possibly unrelated type com.esri.ags.layers:FeatureLayer.   
I have both
            import com.esri.ags.layers.FeatureLayer;
            import com.esri.ags.layers.Layer;



and if I change private var annexLayer:FeatureLayer to just Layer it will compile but the widget will do nothing when clicked. The code that is supposed to open the layer is this...
            protected function widgetOpenedHandler(event:Event):void
            {
    
                map.timeSlider = timeSlider;
    map.addLayer(annexLayer);


    
            }

Any idea what I'm doing wrong?
0 Kudos
ReneRubalcava
Frequent Contributor
Try to cast the layer to coerce the result you want.

Bindable]
private var annexLayer:FeatureLayer = FeatureLayer(map.getLayer("Annexations"));


If you read the docs on the Map::getLayer() method, you'll see it will always return a base Layer that you would need to cast to the proper type in most cases.
0 Kudos
RyanColeman
Occasional Contributor
thanks for the quick reply. That fixes the error but the widget still does nothing when clicked. It doesn't even show a problem in fiddler
0 Kudos