Add labels to a FeatureLayer

Loading...

Note: Support for 3D on mobile devices may vary, view the system requirements for more information.

This sample demonstrates how to add labels to a FeatureLayer in a MapView. You can display labels either by setting the labelInfo property of the FeatureLayer, or by loading a PortalItem with labels already defined.

The value of the MARKER_ACTIVITY field of the FeatureLayer is used to label each feature in the view, which shows Forest Service Recreation Opportunities in the United States.

To add labels to the layer, first we create a LabelClass and assign values for various properties, such as color and font.

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

This sample loads a PortalItem from ArcGIS Online, then adds the label class to the labelingInfo property, and applies a renderer.

map.layers = [
  new FeatureLayer({
    portalItem: { // autocasts as new PortalItem
      id: "6012738cd1c74582a5f98ea30ae9876f"
    },
    labelingInfo: [ labelClass ]
  })
];

Known Limitations

There currently isn't support for labeling polylines in 2D. In 2D MapViews, labeling is only supported for points and polygon centroids.

Sample search results

TitleSample
Loading...