user3297920 user3297920 - 1 month ago 12
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?

jsfiddle: http://jsfiddle.net/9X46z/

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"
};

fl.add(m);

map.addLayer(fl);

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

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

Answer

Here's an updated version with one change: http://jsfiddle.net/2WccC/1/

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

to

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