Select to view content in your preferred language

Labeling FeatureLayer

3336
6
10-27-2011 08:30 AM
KarenRobine
Frequent Contributor
I'm trying to dynamically generate a FeatureLayer and have it automatically labeled. I used a CompositeSymbol as follow.
The correct line, polygon or marker symbol shows but the label doesnt appear.

Ideas?

Code follows:

//Define the Feature Layer:
var url:String = "http://<mymachine>/ArcGIS/rest/services/TestBookmark/MapServer/";
url += txtLayer.text;
featureLayer= new FeatureLayer(url);
featureLayer.id = "Karens Feature Layer";
featureLayer.name = "Karens Feature Layer";
featureLayer.outFields = [txtField.text];
if (featureLayer)
{
featureLayer.addEventListener(LayerEvent.LOAD, featureLayer1_loadHandler);
}

//In load handler:
var fLayer:FeatureLayer = FeatureLayer(event.layer);
var symbol:Symbol = fLayer.symbol;
var txtSymbol:TextSymbol = new TextSymbol();
var txtFormat:TextFormat = new TextFormat();
txtFormat.bold = true;
txtFormat.color = 0x0000FF;
txtFormat.size = 16; //if null, 12 is uses
txtSymbol.textFormat = txtFormat;
txtSymbol.placement = TextSymbol.PLACEMENT_MIDDLE;
txtSymbol.textAttribute = txtField.text;
var compSymbol:CompositeSymbol = new CompositeSymbol();

//following if symbol is line:
var sls:SimpleLineSymbol = new SimpleLineSymbol();
sls.style = SimpleLineSymbol.STYLE_SOLID;
compSymbol.symbols = [sls, txtSymbol];

//apply symbol to layer and add to map
fLayer.symbol = compSymbol;
map.addLayer(fLayer);
Tags (2)
0 Kudos
6 Replies
WilliamBailey
Emerging Contributor
posted same question awhile back... 
http://forums.arcgis.com/threads/41494-Add-labels-to-featureLayer
no replies, I'll be watching here too.
0 Kudos
KarenRobine
Frequent Contributor
Dynamic Feature Layer labeling doesn't seem to be possible. I don't think it's supported in REST, in general.

But you can do it as a Map Tip. I think that's the way I'm going to do this.
0 Kudos
WilliamBailey
Emerging Contributor
I am currently doing just that... Map Tips... problem is Map Tips don't print
I find it hard to believe there is no way to label a FeatureLayer????
0 Kudos
TracySchloss
Honored Contributor
I have been using a PopUpRenderer for labeling my featurelayers.  I have the fields I want in an array.  Then I populating the popup with the fields from it.  Here is the section of code where that is happening:

featureLayer.outFields = outFields.split(",");
var pFieldsArray:Array = new Array();    
 var popUpInfo:PopUpInfo = new PopUpInfo;
  for (var j:Number = 0; j < featureLayer.outFields.length; j++) {
   var pFieldInfo:PopUpFieldInfo
   pFieldInfo = new PopUpFieldInfo();
   pFieldInfo.fieldName = featureLayer.outFields;
   pFieldInfo.label = featureLayer.outFields + ":  ";    
   pFieldInfo.visible=true;
   pFieldInfo.format = new PopUpFieldFormat();
   pFieldsArray.push(pFieldInfo);   
    }    
   popUpInfo.popUpFieldInfos = pFieldsArray;
   
   var popUpRenderer:ClassFactory = new ClassFactory(PopUpRenderer);
   popUpRenderer.properties = {popUpInfo:popUpInfo};     
   featureLayer.infoWindowRenderer = popUpRenderer
;
0 Kudos
AndrewThomas
Deactivated User
We are currently trying to label feature layers also.

The approach we are taking is to add textSymbol graphics to a graphics layer.

Crude code:

    var featureLayer:FeatureLayer = event.target as FeatureLayer;
    var features:ArrayCollection = featureLayer.graphicProvider as ArrayCollection;
                                var labelGraphicsLayer:GraphicsLayer = new GraphicsLayer();
    map.addLayer(labelGraphicsLayer);

    for(var i:int=0;i<features.length;i++)
    {
     var g:Graphic=features.getItemAt(i) as Graphic;
     var x:Number=g.geometry.extent.center.x;
     var y:Number=g.geometry.extent.center.y; 
                                        var geomPoint:MapPoint = new MapPoint(x,y,map.spatialReference);
      var textSymbol:TextSymbol = new TextSymbol();
     textSymbol.color = 0x00ff00;

     textSymbol.text = g.attributes["TITLE"];     
     var textGraphic:Graphic = new Graphic();
     textGraphic.geometry = geomPoint;
     textGraphic.symbol = textSymbol;
     labelGraphicsLayer.add(textGraphic);
                                }


This seems to work but we've still got a bit of work to do around positioning ( no overlapping labels etc)
Having said that, I think I might explore PopUpRenderer as Schlot suggests. That may handle it all better.

If I remember, I'll update this post with our result.

Andrew
Wellington, New Zealand
0 Kudos
TracySchloss
Honored Contributor
The PopUp does just that.  It pops up when the user clicks on a feature.  So if you were wanting to generate labels as you might through the Desktop, you would need to approach it differently.

Tracy
0 Kudos