0

Is it possible to add labels to a 2D FeatureLayer in ArcGIS using the JavaScript 3.25 API. I can only find examples that use Javascript 4.8 and 3D scenes.

The following code doesn't show anything:

layer = new FeatureLayer("https://services3.arcgis.com/ntG7nrNunSl8x2gF/arcgis/rest/services/SmallAreasAgeRanges/FeatureServer/0", {
    "id": "0",
    "outFields": outFields,
    "showLabels": true,
    "labelingInfo": [{
    "labelExpressionInfo": {"value": "Abc"},
        "labelPlacement": "center-center",
        "symbol": new TextSymbol("Abc")
    }]
});

1 Answer 1

2

The snippet below shows how you should be labeling as of version 3.14 to 3.25

require(["esri/layers/LabelClass"], function(LabelClass) { /* code goes here */ });

Example:

//make sure the Map's showLabels option is set to true
var map = new Map("map", {
  basemap: "streets",
  center:[-80, 30],
  zoom: 5,
  showLabels : true
});

//create featurelayer
var flayer = new FeatureLayer(url, {
  outFields: ["*"] //make sure field to label is specified here in outFields
});


//create a text symbol and renderer to define the style of labels
var labelSymbol = new TextSymbol(); 

var json = {
  "labelExpressionInfo": {"value": "{LocalTime}"},
  "useCodedValues": false,
  "fieldInfos": [{fieldName: "LocalTime",format: { "dateFormat": "shortDate"}}]
};

//create instance of LabelClass
var lc = new LabelClass(json);
lc.symbol = labelSymbol; // symbol also can be set in LabelClass' json
flayer.setLabelingInfo([ lc ]);

For reference: Link

1
  • 1
    I had originally tried using the code from that reference page - before asking the question - and it didn't work. It turns out I had been setting 'showLabels' to true in the FeatureLayer not the Map object. Commented Aug 27, 2018 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.