user3297920 user3297920 - 11 months ago 52
Javascript Question

How to label a feature layer that was generated programatically?

I'm new to the ESRI ArcGIS API for Javascript, and I'm trying to generate a feature layer programatically and then add a label layer to it. The feature layer appears to work, but the label layer is providing no output (and no errors, either). Can someone tell me what I'm doing wrong?


Relevant javascript:

var layerDefinition = {
"geometryType": "esriGeometryPoint",
"fields": [
"name": "__OBJECTID",
"alias": "__OBJECTID",
"type": "esriFieldTypeOID",
}, {
"name": "testfield",
"alias": "testfield",
"type": "esriFieldTypeString"

var featureCollection = {
layerDefinition: layerDefinition,
featureSet: null

var fl = new FeatureLayer(featureCollection, {
id: "featurelayer",
outFields: ["testfield"]

var m = new esri.Graphic(new esri.geometry.Point(-77, 40), pushpin);
m.attributes = {
"testfield": "test value"



// create a text symbol to define the style of labels
var testLabel = new TextSymbol().setColor(new Color("#666666"));
testLabelRenderer = new SimpleRenderer(testLabel);

var labels = new LabelLayer({
id: "labels"
labels.addFeatureLayer(fl, testLabelRenderer, "{testfield}");
// add the label layer to the map

Answer Source

Here's an updated version with one change:

var m = new esri.Graphic(new esri.geometry.Point(-77, 40), pushpin);


var m = new esri.Graphic(esri.geometry.geographicToWebMercator(new esri.geometry.Point(-77, 40)), pushpin);