AnsweredAssumed Answered

Does anyone have a good example of adding labels to a feature layer from its service url?

Question asked by cmathers on Jul 24, 2018
Latest reply on Jul 24, 2018 by cmathers

I am trying to use jQuery.getJSON and then taking the JSON object of the label definition to make the label classes but no labels are showing up in my map. getJSON is asynchronous so rather than applying the labels while the layer object is being created I add them to the layers in the map object after everything has been set up using the code below. I take the layer URL from my config, get its definition from the REST endpoint, get the layer from the map object which has the ID of that layer in the config, and then add the labels. Looking at the layer object from the map that is logged to the console at the end they have the labels defined like they should but no labels show up in the map. Is there a better way to apply the labels that I could be doing here?

 

    _.each(appConfig.layerConfig, function(layer){
        if (layer.type == "featurelayer") {
            $.getJSON(layer.url+"?f=json", function(data){
                layerObj = mapObj.findLayerById(layer.id)
                if (data.hasLabels){
                    layerObj.labelsVisible = true;
                    layerObj.labelingInfo = _.map(data.drawingInfo.labelingInfo, function(labelDef){
                        label = LabelClass.fromJSON(labelDef);
                        return label;
                    });
                    console.log(layerObj);
                }
            });
        }
    });

Outcomes