Select to view content in your preferred language

Label features from a featurelayer

669
1
07-11-2012 04:52 AM
roylammers
New Contributor
Hi,

I would like to know how to label the features from a featurelayer.
0 Kudos
1 Reply
GeorgeSimpson
Regular Contributor
Here's the beginning of a way to do it.  This just extends esri.renderer.Renderer by adding a new class "esri.renderer.LabelRenderer" that accepts a symbol and an attribute field.  Like I said, this is a "beginning" so it only adds the labels, it does not combine the labels with the other renderer

//extend the class
dojo.declare("esri.renderer.LabelRenderer", esri.renderer.Renderer, {
                constructor: function (sym,attr) {
                    // 2nd constructor signature added at v2.0:
                    // esri.renderer.SimpleRenderer(<Object> json);

                    if (sym && !sym.declaredClass) {
                        // REST JSON representation
                        var json = sym;
                        sym = json.symbol;

                        if (sym) {
                            this.symbol = esri.symbol.fromJson(sym);
                        }

                        this.label = json.label;
                        this.description = json.description;
                    }
                    else {
                        this.symbol = sym;
                    }

                    this.attributeField = attr;                    
                },

                getSymbol: function (graphic) {
                    this.symbol.text = graphic.attributes[this.attributeField]
                    return this.symbol;
                },

                toJson: function () {
                    return esri._sanitize({
                        type: "label",
                        label: this.label,
                        description: this.description,
                        symbol: this.symbol && this.symbol.toJson()
                    });
                }
            }
);

//add the renderer
var font = new esri.symbol.Font("16pt", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLD, "Helvetica");

 var textSymbol = new esri.symbol.TextSymbol("", font, new dojo.Color("#666633"));
 textSymbol.setOffset(0, 8);

var renderer = new esri.renderer.LabelRenderer(textSymbol,"OBJECTID");
featureLayer.setRenderer(renderer);
0 Kudos