helenchu

Adding callout text to selected feature with an incremental value?

Discussion created by helenchu on Jun 22, 2020
Latest reply on Jun 23, 2020 by helenchu

Hi all,

My ultimate goal to add callout text to my selected features with an incremental value like "sale 1", "sale 2"," sale 3" ect... What I have right now is to set up label to display label in callout text and use this snippet to turn the label on for selected features.  My issue is my label doesn't come from attribute table and each label is different from the others.  How do I modify this code to make it work or if this approach doesn't work in my case, what else could I use to accomplish it?  Thank you very much.

//Layer to label. US Cities lyr was used
      var lyrOfInterest = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
      QueuedTask.Run(() => {
        var selection = MapView.Active.Map.GetSelection();
        //Get the OIDs of the selected features
        //We will use this to label those features only
        var selectedOIDs = selection[lyrOfInterest];
        //Get the layer's definition
        var lyrDefn = lyrOfInterest.GetDefinition() as CIMFeatureLayer;
        if (lyrDefn == null) return;
        //Get the label classes - we need the first one
        //Or you could create a new label class
        var listLabelClasses = lyrDefn.LabelClasses.ToList();
        var theLabelClass = listLabelClasses.FirstOrDefault();
        //set the label class Expression to use the Arcade expression
        theLabelClass.Expression = "$feature.CITY_NAME"; //label using city name
        //Where clause is set to the OIDs of the selected features
        theLabelClass.WhereClause = $"OBJECTID IN ({string.Join(",", selectedOIDs)})";
        //theLabelClass.TextSymbol = Set to any text symbol you want
        //Set the label definition back to the layer.
        lyrOfInterest.SetDefinition(lyrDefn);
        lyrOfInterest.SetLabelVisibility(true);
      });

Outcomes