add callout text symbols to selected features in layout

749
3
Jump to solution
05-29-2020 01:16 PM
helenchu
Occasional Contributor II

All,

I found a snippet for creating callout text symbol below.  What I need is to add callout text symbols to all selected features in layout.  How do I use my selected feature centroid x, y with the codes below ?  Thank you for your help.

private static Task<CIMTextSymbol> CreateBackgroundCalloutAsync() {     return QueuedTask.Run<CIMTextSymbol>(() =>    {         var textSymbol = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlackRGB, 8, "Tahoma", "Bold");         //Create a call out        var backgroundCalloutSymbol = new CIMBackgroundCallout();         //Leader line        //Get a line symbol        var lineSymbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.BlackRGB, 1, SimpleLineStyle.DashDotDot);         //Create a solid fill polygon symbol for the callout.        var aquaBackground = ColorFactory.Instance.CreateRGBColor(190, 255, 232, 100);         var polySymbol = SymbolFactory.Instance.ConstructPolygonSymbol(aquaBackground, SimpleFillStyle.Solid);         //assign the line to the callout        backgroundCalloutSymbol.LeaderLineSymbol = lineSymbol;         //Offset for the text        textSymbol.OffsetX = 10;         textSymbol.OffsetY = 10;         //Assign the polygon to the background callout        backgroundCalloutSymbol.BackgroundSymbol = polySymbol;         //Accent bar        var accentSymbol = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.BlackRGB, 2, SimpleLineStyle.Solid);         backgroundCalloutSymbol.AccentBarSymbol = accentSymbol;         //Set margins for the callout        backgroundCalloutSymbol.Margin = new CIMTextMargin        {             Left = 5,             Right = 5,             Top = 5,             Bottom = 5        };                  //assign the callout to the textSymbol        textSymbol.Callout = backgroundCalloutSymbol;         return textSymbol;     }); }
0 Kudos
1 Solution

Accepted Solutions
helenchu
Occasional Contributor II

That's exactly what I'm looking for.  Thank you very much!

View solution in original post

0 Kudos
3 Replies
UmaHarano
Esri Regular Contributor

You could get the OIDs of the selected features and then pass it to the label class "WhereClause" to label only those features in the map.

Is that what you are looking for?

//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);
      });
helenchu
Occasional Contributor II

That's exactly what I'm looking for.  Thank you very much!

0 Kudos
helenchu
Occasional Contributor II

Hello Uma,

I hope you could help me a bit more regarding labeling issue above.  My end goal is to loop through the selection and assign an incremental # to the label for each selected feature.  Ex: Sale 1, Sale 2, Sale 4. These customized labels won't be from in any attribute field.  How do I do that?  Thanks in advance.

0 Kudos