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

532
4
07-24-2018 08:09 AM
ChrisMathers1
Occasional Contributor II

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);
                }
            });
        }
    });
Tags (2)
0 Kudos
4 Replies
AndyGup
Esri Regular Contributor

Chris, here's a sample app that shows adding labels in 4.8. It uses the following code that includes an Arcade expression. 

      const labelClass = { // autocasts as new LabelClass()
        symbol: {
          type: "text", // autocasts as new TextSymbol()
          color: "green",
          haloColor: "black",
          font: { // autocast as new Font()
            family: "playfair-display",
            size: 12,
            weight: "bold"
          }
        },
        labelPlacement: "above-center",
        labelExpressionInfo: {
          expression: "$feature.MARKER_ACTIVITY"
        }
      };
0 Kudos
ChrisMathers1
Occasional Contributor II

What is a good way to pull the existing label definition from the service though? I have the labels configured at publishing and can access that information from the REST. I am trying to build a reusable app with a simple config file rather than hard coding in this sort of set up.

0 Kudos
AndyGup
Esri Regular Contributor

To get labels to work you need to enable WebGL and it requires AGOL hosting or 10.6.1 for 2D. Does your app meet those conditions?

If those conditions are met and if there is Labeling Info at the time of publishing, it should automatically be read and displayed. Here's a sample app demoing the concept.

And, here is a list of the known WebGL limitations: FeatureLayer | API Reference | ArcGIS API for JavaScript 4.8

ChrisMathers1
Occasional Contributor II

Oh I had seen the WebGL requirement but not the 10.6.1 requirement. Thanks!

0 Kudos