Getting FeatureLayer from ArcGISDynamicMapServiceLayer

3589
7
08-01-2011 05:13 PM
shashinanjaiah
New Contributor
I am trying to get the Featurelayer directly form the map to add an event for infopopup on mouseover. I went through the samples and it works when I create a new instance of the feature layer and add it to the map. It duplicates my layers on the map.

structureFeatureLayer = new FeatureLayer;
    structureFeatureLayer.url = "sampleUrl";
    structureFeatureLayer.mode = "snapshot";
    structureFeatureLayer.outFields = ['FIELD_ID'];
    structureFeatureLayer.addEventListener(GraphicEvent.GRAPHIC_ADD, sLayer_graphicAddHandler);
    map.addLayer(structureFeatureLayer);

//Event Listner when the map is initially loaded
   private function sLayer_graphicAddHandler(event:GraphicEvent):void
   {
    event.graphic.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverStructure);
   }

Can I do this without creating a new instance of feature layer without adding to the map but add an event listener to the existing Map feature layer.

Any help would be appreciated.
Thanks
Tags (2)
0 Kudos
7 Replies
cristiBJ
New Contributor III
Any solution for this? I have the similar question too.
0 Kudos
CedricDespierre_Corporon1
New Contributor
Hello,
You can access it by using map.getLayer('featurelayerid')

http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/Map.html#getLayer()
0 Kudos
AndrewThomas
New Contributor II
I'm having issues here too? Any solution to this?

How do we get from map.getLayer(DynamicMapServiceLayerID) to a feature layer?

Ideally something like this: (which doesn't work)
var myfLayer:FeatureLayer = map.getLayer(DynamicMapServiceLayerID) as FeatureLayer;


or perhaps:
var myfLayer:FeatureLayer = map.getLayer(DynamicMapServiceLayerID)[0] as FeatureLayer;


In my case I know the layer number I want
I.E.

map.getLayer(DynamicMapServiceLayerID).url + "/0"


So my interim solution is:

var layer:Layer = map.getLayer("Metadata");

var myfLayer:FeatureLayer = new FeatureLayer(layer.url + "/0");
myfLayer.id = "MetadataFeatureLayer";
myfLayer.addEventListener(GraphicEvent.GRAPHIC_ADD,fLayer_graphicAddHandler);
map.addLayer(myfLayer);


but I want to do this without adding another layer to the map.

Andrew
0 Kudos
IvanBespalov
Occasional Contributor III
Andrew,
but I want to do this without adding another layer to the map.


var layer:Layer = map.getLayer("Metadata");

var myfLayer:FeatureLayer = new FeatureLayer(layer.url + "/0");
myfLayer.id = "MetadataFeatureLayer";
myfLayer.addEventListener(GraphicEvent.GRAPHIC_ADD,fLayer_graphicAddHandler);
// add layer/table load event listener
myfLayer.addEventListener(LayerEvent.LOAD, onLayerLoad, false, 0, true);
dispatchEvent(new LayerEvent(LayerEvent.LOAD, myfLayer)); // the code you looked for
// map.addLayer(myfLayer);

...

/*
 * Listen layer load handler
 */
protected function onLayerLoad(event:LayerEvent):void
{
    // now you can work with loaded layer/table
}


Good luck.
0 Kudos
AndrewThomas
New Contributor II
Hmmmm....

Thanks, but it wasn't the result I was hoping for. Although I think it's a case of me 'having my cake and eating it too' as they say.

The fLayer_graphicAddHandler is never being run. I guess because the layer isn't actually being added to the map.

Thanks anyway.

Andrew
0 Kudos
IvanBespalov
Occasional Contributor III
Andrew,

I do not understand what you want to hear, listening myfLayer.addEventListener(GraphicEvent.GRAPHIC_ADD,fLayer_graphicAddHandler);?
How are going to add "Graphic"?

As I understand, for FeatureLayer add() is not supported.

What is your task? Why fLayer_graphicAddHandler needed?

<?xml version="1.0" encoding="utf-8"?>
<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"
                    backgroundColor="0x9c9c9c">
 
 <!-- Adobe Flex SDK 4.6.0 -->
 <!-- ArcGIS API for Flex 3.0 prerelease 15.03.2012 -->
 <!-- web.zone.ee/bespiva/layerwithoutmap -->
 
 <s:layout>
  <s:VerticalLayout gap="10"
     paddingLeft="10"
     paddingRight="10"
     paddingTop="10"
     paddingBottom="10" />
 </s:layout>
 
 <s:Label text="Load layer:" />
 
 <s:HGroup width="100%">
  
  <s:TextInput id="txtUrl"
      width="100%"
      text="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer/0" />
  
  <s:Button label="Load"
         click="onLoadButtonClick(event)" />
  
 </s:HGroup>
 
 <s:Label text="Query results table:" />
 
 <s:VGroup width="100%"
         height="50%">
  
  <s:DataGrid width="100%"
     height="100%" 
     dataProvider="{gridProvider}" />
  
 </s:VGroup>
 
 <s:Label text="Logs:" />
 
 <s:VGroup width="100%"
     height="100%">
  
  <s:TextArea width="100%"
     height="100%"
     id="log" />
  
 </s:VGroup>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.events.LayerEvent;
   import com.esri.ags.layers.FeatureLayer;
   import com.esri.ags.tasks.supportClasses.Query;
   
   import mx.collections.ArrayCollection;
   import mx.rpc.AsyncResponder;
   import mx.rpc.Fault;
   import mx.utils.StringUtil;
   
   [Bindable]
   private var gridProvider:ArrayCollection = new ArrayCollection();
   
   private var activeLayer:FeatureLayer = null;
   
   private function addLog(message:String):void
   {
    log.text = StringUtil.substitute("{0}\n\n{1}", message, log.text);
   }
   
   protected function onLoadButtonClick(event:MouseEvent):void
   {
    addLog("onLoadButtonClick()");
    
    if (txtUrl.text.length > 0)
    {
     initLayer(txtUrl.text);
    }
    else
    {
     addLog("Layer url is empty. Enter valid layer url.");
    }
   }
   
   private function initLayer(url:String):void
   {
    addLog("initLayer()");
    if (activeLayer)
    {
     gridProvider = null;
     activeLayer.removeEventListener(LayerEvent.LOAD, onLayerLoad);
     activeLayer.removeEventListener(LayerEvent.LOAD_ERROR, onLayerLoadError);
    }
    
    activeLayer = new FeatureLayer(url);
    activeLayer.mode = FeatureLayer.MODE_SNAPSHOT;
    activeLayer.outFields = new Array("*"); // all
    
    activeLayer.addEventListener(LayerEvent.LOAD, onLayerLoad, false, 0, true);
    activeLayer.addEventListener(LayerEvent.LOAD_ERROR, onLayerLoadError, false, 0, true);
    
    dispatchEvent(new LayerEvent(LayerEvent.LOAD, activeLayer));
   }
   
   protected function onLayerLoad(event:LayerEvent):void
   {
    addLog("onLayerLoad()");
    
    queryFeatures();
   }
   
   protected function onLayerLoadError(event:LayerEvent):void
   {
    addLog("onLayerLoadError()");
   }
   
   private function queryFeatures():void
   {
    addLog("queryFeatures()");
    if (activeLayer)
    {
     var query:Query = new Query();
     query.returnGeometry = false;
     query.where = "1=1"; // all
     
     activeLayer.queryFeatures(query, new AsyncResponder(onQueryResult, ouQueryFault, activeLayer));
    }
   }
   
   protected function onQueryResult(featuresSet:FeatureSet, token:Object = null):void
   {
    addLog("onQueryResult()");
    var queryLayer:FeatureLayer = activeLayer as FeatureLayer;
    if (activeLayer != queryLayer)
    {
     return;
    }
    
    gridProvider = new ArrayCollection();
    for each (var gr:Graphic in featuresSet.features)
    {
     gridProvider.addItem(gr.attributes);
    }
   }
   
   protected function ouQueryFault(fault:Fault, token:Object = null):void
   {
    addLog(StringUtil.substitute("ouQueryFault()\n{0}", fault.message.toString()));
   }
   
  ]]>
 </fx:Script>
 
</s:Application>
0 Kudos
AndrewThomas
New Contributor II
ibespalov,

I think I'm wishing for something that isn't possible.

Just a bit of Background:
I'm trying to replicate this below in SampleFlexViewer:
Sample feature layer based on mapserver layer

But since the mapservice Layer in question is already loaded/added to the map object, I'm trying to get away with not adding it again to the map as a feature layer.

At the moment I am adding it as a feature layer with null symbol style and null outline, so the user can't actually tell that it's been added twice.

When I tried your code above, the fLayer_graphicAddHandler event is never fired. I guess this can't be fired until features(graphics) are added to the map. and this can't happen if the featurelayer isn't added to the map.


I think I will end up using your code for something else I want to do though, so thanks.

Thanks

Andrew
0 Kudos