Select to view content in your preferred language

Add a new FeatureLayer dynamicly

2457
5
07-16-2011 08:24 AM
SiminYou
Deactivated User
I am trying to add a FeatureLayer using
var myLayer:FeatureLayer = new FeatureLayer("http://myserver/FeatureServer/0");
   pointLayer.mode = "onDemand";
   pointLayer.outFields = ["*"];
   map.addLayer(myLayer);

instead of [HTML]<esri:FeatureLayer id="myLayer "
         url="http://myserver/FeatureServer/0"
         mode="onDemand"
         outFields="*"/>[/HTML]
However, the second one works but the first failed. Nothing shows when I use the first one to add a new layer.
Can anyone help me????Thanks!!
Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Simin,

    You have a mixture of variable names in your action script code...

var myLayer:FeatureLayer = new FeatureLayer("http://myserver/FeatureServer/0"); 
pointLayer.mode = "onDemand";
pointLayer.outFields = ["*"];
map.addLayer(myLayer);
And doesn't your url contain arcgis/rest? like this?
http://myserver/arcgis/rest/services/FeatureServer/0
0 Kudos
SiminYou
Deactivated User
Thanks for your reply.

I am sorry I mistyped some variables here..
the url contains arcgis/rest, and the url works well when I use the xml-style representation, but doesn't work when I add the feature layer in the action script code.

BTW, I use Bing map as the base map and try to add a feature layer on the top of the Bing map.


Simin,

    You have a mixture of variable names in your action script code...

var myLayer:FeatureLayer = new FeatureLayer("http://myserver/FeatureServer/0"); 
pointLayer.mode = "onDemand";
pointLayer.outFields = ["*"];
map.addLayer(myLayer);
And doesn't your url contain arcgis/rest? like this?
http://myserver/arcgis/rest/services/FeatureServer/0
0 Kudos
SiminYou
Deactivated User
Here is a test I made, in the action script code, I add a feature layer, but when the program start running, nothing shows up. However, when the layer is added in the xml code, the feature layer shows correctly on the map.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:esri="http://www.esri.com/2008/ags"
      initialize="application1_initializeHandler(event)">
 <fx:Script>
  <![CDATA[
   import com.esri.ags.layers.FeatureLayer;
   import mx.events.FlexEvent;
   
   protected function application1_initializeHandler(event:FlexEvent):void
   {
    // This one doesn't work
//    var pointLayer:FeatureLayer = new FeatureLayer("http://myserver/ArcGIS/rest/services/Test/FeatureServer/1");
//    pointLayer.mode = "onDemond";
//    pointLayer.outFields = ["*"];
//    map.addLayer(pointLayer);
   }
  ]]>
 </fx:Script>
 <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
 </fx:Declarations>
 <esri:Map id="map">
  <!--This one works-->
  <esri:FeatureLayer id="pointLayer"
         url="http://myserver/ArcGIS/rest/services/Test/FeatureServer/1"
         mode="onDemand"
         outFields="*"
         />
 </esri:Map>
</s:Application>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Simin,

  Try this:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                 
                           xmlns:s="library://ns.adobe.com/flex/spark"                 
                           xmlns:mx="library://ns.adobe.com/flex/mx" 
                           minWidth="955" minHeight="600" 
                           xmlns:esri="http://www.esri.com/2008/ags"                
                           creationComplete="init()">
              <fx:Script>
                  <![CDATA[
                      import com.esri.ags.layers.FeatureLayer;
                      import mx.events.FlexEvent;

                      protected function init():void{
                          var pointLayer:FeatureLayer = new FeatureLayer("http://myserver/ArcGIS/rest/services/Test/FeatureServer/1");                           pointLayer.mode = "onDemand";
                          pointLayer.outFields = ['*'];
                          map.addLayer(pointLayer);
                     }
                  ]]>
             </fx:Script>
            <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
            </fx:Declarations>
            <esri:Map id="map" />
</s:Application>
0 Kudos
SiminYou
Deactivated User
Thanks so much, Robert.
I didn't realize I have so many typos.. Anyway, it works now.

Simin,

  Try this:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                 
                           xmlns:s="library://ns.adobe.com/flex/spark"                 
                           xmlns:mx="library://ns.adobe.com/flex/mx" 
                           minWidth="955" minHeight="600" 
                           xmlns:esri="http://www.esri.com/2008/ags"                
                           creationComplete="init()">
              <fx:Script>
                  <![CDATA[
                      import com.esri.ags.layers.FeatureLayer;
                      import mx.events.FlexEvent;

                      protected function init():void{
                          var pointLayer:FeatureLayer = new FeatureLayer("http://myserver/ArcGIS/rest/services/Test/FeatureServer/1");                           pointLayer.mode = "onDemand";
                          pointLayer.outFields = ['*'];
                          map.addLayer(pointLayer);
                     }
                  ]]>
             </fx:Script>
            <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
            </fx:Declarations>
            <esri:Map id="map" />
</s:Application>
0 Kudos