Select to view content in your preferred language

(beginner) How to add a featurelayer via a widget to the flexviewer?

2289
7
Jump to solution
03-12-2012 03:44 AM
LeenD_hondt
Occasional Contributor
Hi,

I want to load a layer as a featurelayer in my widget.

WHAT WORKED:
Outside flexviewer, in a standalone mxml, I managed to call the layer via the following way:
<esri:FeatureLayer id="fLayer" url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"  load="fLayer_loadHandler(event)" mode="snapshot"/>

In this case, once the layer was loaded, I could define the timesliderstopsbyTimeInterval from the timeslider using parameters coming from the layer.
protected function fLayer_loadHandler(event:LayerEvent):void
   {
    var timeInfo:TimeInfo=fLayer.layerDetails.timeInfo;
    var timeInterval:int=timeInt.value;
    myTimeSlider.createTimeStopsByTimeInterval(timeInfo.timeExtent,timeInterval,timeInfo.timeIntervalUnits);


Now, I want to integrate this in the flexviewer as a widget which calls a layer and let a timeslider work on that layer.
But how do I call this featurelayer from within my widget mxml?

WHAT DOES NOT WORK:
I tried:
In my config file, I have mentioned the layer under the group of operational layers:
<layer label="GPS Track" type="dynamic" visible="false" id="track"  url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"/>
When I put "visible" on true, it shows in the flexviewer -> there is no issue. But, I want the layer to be invisible untill the user clicks on the widget for the timeslider.

In my widget.mxml I tried:
protected function widgetOpenedHandler(event:Event):void
   {
    map.timeSlider = myTimeSlider;
    var fLayer:FeatureLayer = map(track) as FeatureLayer;
    fLayer.mode="snapshot";
    fLayer.load=LoaderEvent (? and then?) 
       
   }

But he informs that 'access of undefined property 'track'.

So I want to do exactly the same as in the standolane mxml, but from the widget:
-on click -> the layer (with id="track" in the config file) should be loaded -> when loaded the createtimestopinterval should be created (I use for that the name "flayer")


Thanks a lot in advance for any help!
Leen
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LeenD_hondt
Occasional Contributor
Thanks a lot! I'm starting to see some light! 🙂

From reference addLayer:


if you find MapManager.mxml in Flex Viewer application (v 2.5) sources (~ line 620) - layer id is defined and it is the same as layer name
var featureLayer:FeatureLayer = new FeatureLayer(url); ... featureLayer.id = label; // <layer label="Test" ...> in config.xml featureLayer.name = label; // <layer label="Test" ...> in config.xml ... map.addLayer(featureLayer); ...


In your situation:
var layerId:String="Test"; // <layer label="Test" type="feature" ...> in config.xml var myLayer:FeatureLayer = map.getLayer(layerId) as FeatureLayer;

View solution in original post

0 Kudos
7 Replies
LeenD_hondt
Occasional Contributor
Hi, I would like to simplify my question:

-I have defined a layer in my config.xml and gave it an 'id':
<operationallayers>
<layer label="Test" type="feature"  id="idlayer"              
url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"/> 
 

-Now, in my widget.mxml, I want to go and get this layer identified in the config.xml.
First I created a variable:
[bindable] public var fLayer:FeatureLayer; 

Then, I would like to get the featurelayer defined in my config.xml, I tried:
   protected function widgetOpenedHandler(event:Event):void
   {
    map.timeSlider = myTimeSlider;
    fLayer= map.getLayer(idlayer);
    fLayer.mode = "snapshot";
        
    var timeInfo:TimeInfo=fLayer.layerDetails.timeInfo; 
    var timeInterval:int=timeInt.value;
    myTimeSlider.createTimeStopsByTimeInterval(timeInfo.timeExtent,timeInterval,timeInfo.timeIntervalUnits);
    
   }...

But it does not work and returns the problem "Layer to possibly unrelated type com.esri.ags.layers.featurelayers

What do I do wrong? In the API reference for the function 'getlayer' is mentioned getlayer(layerid:string)
Thanks!

Hi,

I want to load a layer as a featurelayer in my widget.

WHAT WORKED:
Outside flexviewer, in a standalone mxml, I managed to call the layer via the following way:
<esri:FeatureLayer id="fLayer" url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"  load="fLayer_loadHandler(event)" mode="snapshot"/>

In this case, once the layer was loaded, I could define the timesliderstopsbyTimeInterval from the timeslider using parameters coming from the layer.
protected function fLayer_loadHandler(event:LayerEvent):void
   {
    var timeInfo:TimeInfo=fLayer.layerDetails.timeInfo;
    var timeInterval:int=timeInt.value;
    myTimeSlider.createTimeStopsByTimeInterval(timeInfo.timeExtent,timeInterval,timeInfo.timeIntervalUnits)

Now, I want to integrate this in the flexviewer as a widget which calls a layer and let a timeslider work on that layer.
But how do I call this featurelayer from within my widget mxml?

WHAT DOES NOT WORK:
I tried:
In my config file, I have mentioned the layer under the group of operational layers:
<layer label="GPS Track" type="dynamic" visible="false" id="track"  url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"/>
When I put "visible" on true, it shows in the flexviewer -> there is no issue. But, I want the layer to be invisible untill the user clicks on the widget for the timeslider.

In my widget.mxml I tried:
protected function widgetOpenedHandler(event:Event):void
   {
    map.timeSlider = myTimeSlider;
    var fLayer:FeatureLayer = map(track) as FeatureLayer;
    fLayer.mode="snapshot";
    fLayer.load=LoaderEvent (? and then?) 
       
   }

But he informs that 'access of undefined property 'track'.

So I want to do exactly the same as in the standolane mxml, but from the widget:
-on click -> the layer (with id="track" in the config file) should be loaded -> when loaded the createtimestopinterval should be created (I use for that the name "flayer")


Thanks a lot in advance for any help!
Leen
0 Kudos
IvanBespalov
Frequent Contributor
How to GET:
private var myLayer:FeatureLayer = null;
private var myLayerName:String = "Test"; // defined in config.xml <layer label="Test"... initialized in MapManager.mxml

protected function onWidgetOpened(event:Event):void
{
    if (!myLayer) // if (myLayer == null)
    {
        initializeMyLayer();
    }  
}

private function initializeMyLayer():void
{
    if (map)
    {
        var layer:Layer = null;             
        for each (layer in map.layers)
        {
            if (layer is FeatureLayer && layer.name == myLayerName) //any other conditions (id, visibility, loaded or not, ...)
            {
                myLayer = layer as FeatureLayer;
         break;
            }
        }
    }
}


How to ADD:
private var myLayer:FeatureLayer = null;
private var myLayerName:String = "Test"; // defined in config.xml <layer label="Test"... initialized in MapManager.mxml

protected function onWidgetOpened(event:Event):void
{
    if (!myLayer) // if (myLayer == null)
    {
        initializeMyLayer();
    }  
}

private function initializeMyLayer():void
{
    if (map)
    {
        var layer:Layer = null;             
        for each (layer in map.layers)
        {
            if (layer is FeatureLayer && layer.name == myLayerName) //any other conditions (id, visibility, loaded or not, ...)
            {
                myLayer = layer as FeatureLayer;
                break;
            }
        }

        if (!myLayer)
        {
             addMyLayer();
        }
    }
}

private function addMyLayer():void
{
    if (map)
    {
        myLayer = new FeatureLayer("serviceUrl");
        // set other needed params (http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/FeatureLayer.html)
        var myLayerId:String = map.addLayer(myLayer);
        trace("Feature layer added. Layer id=" + myLayerId);
    }
}




As you can read in documentation (Flex Viewer version 2.5) about config.xml layer tag (if you can read), <layer> has no id attribute.
wrong code 
... 
<operationallayers> 
<layer label="Test" type="feature"   id="idlayer"   
url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"/>


So what documentation you read? Can you provide me with link.
0 Kudos
LeenD_hondt
Occasional Contributor
Dear,

Thanks. But would it be possilbe to explain me what the code you inserted does? I do not fully understand.

In my code I tried to call the layer via:
protected function widgetOpenedHandler(event:Event):void
   {
    map.timeSlider = myTimeSlider;
    fLayer= map.getLayer(idlayer);
    fLayer.mode = "snapshot";
        
    var timeInfo:TimeInfo=fLayer.layerDetails.timeInfo; 
    var timeInterval:int=timeInt.value;
    myTimeSlider.createTimeStopsByTimeInterval(timeInfo.timeExtent,timeInterval,timeInfo.timeIntervalUnits);
    
   }


I understand now that layer does not have the attribute 'id'. I thought it did because of the syntax mentioned in http://help.arcgis.com/en/webapi/flex/apiref/index.html. It is mentioned:

getLayer () method  
public function getLayer(layerId:String):Layer
Returns an individual layer of a map.
Parameters
layerId:String �?? The layer identifier. 
Returns Layer �?? The layer associated with ID. If not found, null is returned. 


-> but i understand now that it only existis for sublayers. However, can you help me with the code you wrote?
1) so it cannot work with "getlayer"?
2) the code is not working (I only renamed myLayer by fLayer not having to change it everywhere in my mxml. But I get the error on the line "var layer:Layer = null;"
error: Type was not found or was not a compile type constant: Layer.

My code:
In the config.xml:
<operationallayers>
<layer label="Test" type="feature"  url="http://trainingcloud.arcgis.com/ArcGIS/rest/services/Redlands_Collisions2008/MapServer/0"/>   


In the mxml:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
       xmlns:s="library://ns.adobe.com/flex/spark"
       xmlns:mx="library://ns.adobe.com/flex/mx"
       xmlns:viewer="com.esri.viewer.*"
       xmlns:esri="http://www.esri.com/2008/ags">
 <fx:Script>
  <![CDATA[
   import com.esri.ags.components.supportClasses.IntegerField;
   import com.esri.ags.events.LayerEvent;
   import com.esri.ags.layers.FeatureLayer;
   import com.esri.ags.layers.supportClasses.TimeInfo;
   import flashx.textLayout.tlf_internal;
   import mx.events.ItemClickEvent;
   import org.osmf.events.LoaderEvent;
   import spark.events.TrackBaseEvent; 
   import com.esri.ags.layers.supportClasses.TimeInfo;
   
   [bindable] private var fLayer:FeatureLayer = null;
   private var fLayerName:String = "Test"; // defined in config.xml <layer label="Test"... initialized in MapManager.mxml
    
   protected function onWidgetOpened(event:Event):void
   {
    if (!fLayer) // if (fLayer == null)
    {
     initializeMyLayer();
    }  
   }
   
   private function initializeMyLayer():void
   {
    if (map)
    {
     var layer:Layer = null; //here the error is given            
     for each (layer in map.layers)
     {
      if (layer is FeatureLayer && layer.name == fLayerName) //any other conditions (id, visibility, loaded or not, ...)
      {
       fLayer = layer as FeatureLayer;
       fLayer.mode="snapshot";
       
       var timeInfo:TimeInfo=fLayer.layerDetails.timeInfo; 
       var timeInterval:int=timeInt.value;
       myTimeSlider.createTimeStopsByTimeInterval(timeInfo.timeExtent,timeInterval,timeInfo.timeIntervalUnits);
       break;
      }
     }
    }
   }


Thanks again!
Leen

private var myLayer:FeatureLayer = null;
private var myLayerName:String = "Test"; // defined in config.xml <layer label="Test"... initialized in MapManager.mxml

protected function onWidgetOpened(event:Event):void
{
    if (!myLayer) // if (myLayer == null)
    {
        initializeMyLayer();
    }  
}

private function initializeMyLayer():void
{
    if (map)
    {
        var layer:Layer = null;             
        for each (layer in map.layers)
        {
            if (layer is FeatureLayer && layer.name == myLayerName) //any other conditions (id, visibility, loaded or not, ...)
            {
                myLayer = layer as FeatureLayer;
         break;
            }
        }
    }
}


As you can read in documentation (Flex Viewer version 2.5) about config.xml layer tag (if you can read), <layer> has no id attribute.


So what documentation you read? Can you provide me with link.
0 Kudos
IvanBespalov
Frequent Contributor
Leen,
1-
so it cannot work with "getlayer"?

No, map.getLayer(layerid) works, if you know valid layer id, do you know it?
The function getLayer is not the problem - it works as all other functions.
The problem is: you do not know valid layer id -> actual questions are:
"How can i get valid layer id?" (map.layerIds)
or
"What other ways to get layer from map?" (my last post)


2-
Type was not found or was not a compile type constant: Layer.

Think before you write :confused:
import com.esri.ags.layers.Layer;


Read documentation of...

hei, Leen do you understand what difference between ArcGIS API (application programming interface) fo Flex vs. ArcGIS Viewer for Flex?
The Viewer for Flex is just sample application. (With great functionality and interface, extendable ... but still sample application, better one, but still...)

The confix.xml <layer> tag in Viewer for Flex and Layer in Flex API reference are not equals things.

Good luck. 😄
0 Kudos
LeenD_hondt
Occasional Contributor
Thanks again! I was indeed confused with the 'layer' tag used in Flex viewer and the 'layer' in API for flex (clearing up a lot now).

For the map.getlayer (I'd really like to use this way if possible)  -> The question was exactly how to find this one? What is this 'layerid' exactly? How can you know it/define it?
Thanks a million!

Leen,
1-
No, map.getLayer(layerid) works, if you know valid layer id, do you know it?


2-
Think before you write :confused:
import com.esri.ags.layers.Layer;


Read documentation of...

hei, Leen do you understand what difference between ArcGIS API (application programming interface) fo Flex vs. ArcGIS Viewer for Flex?
The Viewer for Flex is just sample application. (With great functionality and interface, extendable ... but still sample application, better one, but still...)

The confix.xml <layer> tag in Viewer for Flex and Layer in Flex API reference are not equals things.

Good luck. 😄
0 Kudos
IvanBespalov
Frequent Contributor
From reference addLayer:
...Adds a layer to the map. If an ID was not defined, a unique name will be created and assigned to the layer. The unique name is created using NameUtil.createUniqueName, which will combine the unqualified class name with an incrementing counter to create something like "Layer2"...


if you find MapManager.mxml in Flex Viewer application (v 2.5) sources (~ line 620) - layer id is defined and it is the same as layer name
var featureLayer:FeatureLayer = new FeatureLayer(url);
...
featureLayer.id = label; // <layer label="Test" ...> in config.xml
featureLayer.name = label; // <layer label="Test" ...> in config.xml
...
map.addLayer(featureLayer);
...


In your situation:
var layerId:String="Test"; // <layer label="Test" type="feature" ...> in config.xml
var myLayer:FeatureLayer = map.getLayer(layerId) as FeatureLayer;
0 Kudos
LeenD_hondt
Occasional Contributor
Thanks a lot! I'm starting to see some light! 🙂

From reference addLayer:


if you find MapManager.mxml in Flex Viewer application (v 2.5) sources (~ line 620) - layer id is defined and it is the same as layer name
var featureLayer:FeatureLayer = new FeatureLayer(url); ... featureLayer.id = label; // <layer label="Test" ...> in config.xml featureLayer.name = label; // <layer label="Test" ...> in config.xml ... map.addLayer(featureLayer); ...


In your situation:
var layerId:String="Test"; // <layer label="Test" type="feature" ...> in config.xml var myLayer:FeatureLayer = map.getLayer(layerId) as FeatureLayer;
0 Kudos