how to label layers using LayerDrawingOptions

2080
8
09-16-2014 09:05 PM
VikramS
Occasional Contributor

Hello All,

I am trying to display the labels in the client side using LayerDrawingOptions . My actual requirement is to turn on/off labels in a layer . I believe it is only possible using Label class and LayerDrawingoptions . Below is my code but the label doesn't display. .

When I check the property . labelPlacement is always null . Can anybody tell me what am I missing . Thanks

LabelId.jpg

var textSymbol = new esri.symbol.TextSymbol({

                font: new esri.symbol.Font("18", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLD, "Helvetica"),

                color: new esri.Color("#666633")

            });

            var labelClass = new esri.layers.LabelClass({

                labelExpression: "[SeriesID]",

                labelPlacement: "above-along",

                symbol:textSymbol

            });

            var layerDrawingOption = new esri.layers.LayerDrawingOptions({

                labelingInfo: [labelClass],

                showLabels: true,

transparency:50

            });

    dynamicLayer.setLayerDrawingOptions(layerDrawingOptions, true);

Tags (1)
0 Kudos
8 Replies
RiyasDeen
Occasional Contributor III

Hi Vikram,

setLayerDrawingOptions, accepts an array of LayerDrawingOptions arcgisdynamicmapservicelayer-amd | API Reference | ArcGIS API for JavaScript .

Try changing as below,

var options = [];

options[<<Layer ID for which you want to assign the drawing option>>] = layerDrawingOptions;

dynamicLayer.setLayerDrawingOptions(options, true);

You may want to change true to false, to refresh the map.

0 Kudos
VikramS
Occasional Contributor

Hello Riyas,

I did what you suggested . But still I cannot see the labels .

0 Kudos
RiyasDeen
Occasional Contributor III

Hi Vikram,

Have a look at Edit fiddle - JSFiddle , the issue is labelPlacement values mentioned in documentation is not correct or is not working. I have placed the values that should be used for labelPlacement in the fiddle.

Looks like incorrect documentation. Esri Technical Support‌ may want to have a look at it.

0 Kudos
DavidColey
Frequent Contributor

Hi Vikram, as you know the label layer must be drawn from the same feature layer that you wish to label.  The label layer visibility is controlled by the feature layer visibility.  If the feature layer is not visible, then neither is the label layer.  If you want to be able to turn the label layer on/off, then you have to set up some kind of control to do so.  What we did was to use Nianwei Liu's Table of Contents dijit.  Here's some code, hope this helps:

/*create the layer*/

var hghColor = new Color("#004DA8");

  var hghLabel = new TextSymbol().setColor(hghColor);

  hghLabel.font.setSize("12").setFamily("arial").setWeight(Font.WEIGHT_BOLD);

  var hghRenderer = new SimpleRenderer(hghLabel);

  var hLabelLayer = new LabelLayer();

  hLabelLayer.addFeatureLayer(lyrHighZones, hghRenderer, "{SCHOOL_NAME}", {howManyLabels: "ManyLabels"});

  hLabelLayer.id = "HighLabels";

  hLabelLayer.minScale =  72223;

  hLabelLayer.setVisibility(true);

/*add to toc*/

/* Table Of Contents */

  mapMain.on("layers-add-result", function(evt) {

  toc = new TOC({

  map: mapMain,

  layerInfos: [{

  layer:  hLabelLayer,

  title: "High School Labels"

}]

}, "tocDiv");

toc.startup();

/*add the layers*/

mapMain.addLayers([hLabelLayer, lyrHighZones]);

0 Kudos
JssrRR
by
Occasional Contributor II

Hi,

I am having a problems with the feature layer not showing up after I add label layer to my application, it is working fine, doing everything required but displaying the points. I know they are loaded because when I draw a circle, the points within the the buffer are selected and sent to the datagrid, I want to be able see all points along with the FacilityIDs labeled.

Here is my code in the attached JSfiddle:

http://jsfiddle.net/JSSR/zbLdk6q4/1/

If I add this everything  goes away

map.addLayer(featureLayer);

or this

featureLayer.show();

may be I am not putting in the right place.

Thanks!

0 Kudos
HaroldBostic
Occasional Contributor II

Sorry I don't have the solution, but the problem is with your setRenderer

I commented out line 141 //featureLayer.setRenderer(NFRenderer); and your point show up with their REST renderer

check out this Fiddle

NF ID labels showing up - JSFiddle

JssrRR
by
Occasional Contributor II

Hi Harold,

The points show up now, even in my original code when I comment out the line 141, and it is doing what I am looking for.

Thanks for looking at it and pointing out the problem.

Will look into the setRenderer and see what is the problem and if needs any modifications.

Thanks again for the help!

0 Kudos
JssrRR
by
Occasional Contributor II

Harold,

I fixed the setRenderer, here is a working version of my application, labels are now showing up, at the scale I want:

http://jsfiddle.net/JSSR/d6z1enf3/

Thanks for pointing out the problem!

0 Kudos