Select to view content in your preferred language

Labels not showing for Graphics in GraphicOverlay

273
3
Jump to solution
03-25-2025 05:17 AM
ViktorSafar
Frequent Contributor

Here is my label definition 

    public LabelDefinition GetLabelDefinition()
    {
        var textSymbol = new TextSymbol
        {
            Color = System.Drawing.Color.Black,
            Size = 40,
            FontWeight = FontWeight.Bold,
            HaloColor = System.Drawing.Color.White,
            HaloWidth = 3,
        };
        var def = new LabelDefinition(new SimpleLabelExpression("Ahoj"), textSymbol)
        {
            Placement = Esri.ArcGISRuntime.ArcGISServices.LabelingPlacement.PointCenterRight,
            MinScale = 0,
            MaxScale = double.MaxValue,
        };

        return def;
    }

Here is my overlay 

_workOrdersOverlay = new GraphicsOverlay
{
	Id = AppConstants.WorkOrdersLayerId,
	Renderer = _workOrdersLayerService.GetRenderer(),
	LabelsEnabled = true,
};
_workOrdersOverlay.LabelDefinitions.Add(_workOrdersLayerService.GetLabelDefinition());

Here is my overlay with Graphics and no labels.

ViktorSafar_0-1742904693043.png

If I click on one of the symbols and inspect the result of GeoViewController.IdentifyGraphicsOverlaysAsync I can see the overlay labeling is correctly configured

resultsPerOverlay.First().GraphicsOverlay.LabelsEnabled
true
resultsPerOverlay.First().GraphicsOverlay.LabelDefinitions.First().Expression
{Esri.ArcGISRuntime.Mapping.Labeling.SimpleLabelExpression}
    Expression: "Ahoj"

 

What am I missing?

 

0 Kudos
1 Solution

Accepted Solutions
pnarkhede
Esri Contributor

Hi @ViktorSafar,

Thank you for providing the repro.

The issue with labels not displaying in your GraphicsOverlay appears to be related to how the label expression is defined. When using a SimpleLabelExpression with static text like "Ahoj" or "Some text", the labels won't display because the expression isn't properly linked to attributes in your graphics.

For labels to display correctly in a GraphicsOverlay, you need to:

 

1. Make sure your label expression references an attribute that exists in your graphics. Instead of:

new SimpleLabelExpression("Some text")

Try using:

new SimpleLabelExpression("[name]")

 

2. Ensure your graphics have the corresponding attribute populated:

var g = new Graphic() { Geometry = new MapPoint(0, 0) };
g.Attributes.Add("name", "Homer");
 
These changes should help your labels display correctly. Let me know If you have any further questions or need additional assistance.
 
 
Regards,
Prathamesh

View solution in original post

0 Kudos
3 Replies
pnarkhede
Esri Contributor

Hi,

Thank you for your question. I can see a few potential reasons why your labels might not be showing up:

  • Label Expression: Your label definition uses a static text "Ahoj" for all features. This means all your graphics will have the same label. Usually, you'd want to use an attribute from your graphics, like "[SomeField]" or "{SomeField}". If you intended to have dynamic labels, you should modify your expression to use a field from your graphics' attributes.
new SimpleLabelExpression("[placeholder]")​

 

  • Graphic Attributes: Ensure your graphics have attributes that match the label expression. If you're using a static text, this isn't necessary, but it's a good practice for dynamic labeling.
myGraphic.Attributes["placeholder"] = "Ahoj";​

 

  • Scale Range: Your current setup has the min and max scales reversed. According to the ArcGIS Runtime SDK specification, you should set:
    • MinScale to a larger value (e.g., 1000000)
    • MaxScale to a smaller value (e.g., 0)

If you continue to have issues, ensure that your graphics are correctly added to the overlay and that there are no other visual elements obscuring the labels.

Let me know if these changes help!

 

Regards,
Prathamesh

0 Kudos
ViktorSafar
Frequent Contributor

Hi @pnarkhede 

I simplified the code example to use static text after I had struggled with the labels not showing with an ArcadeLabelExpression. But it made no difference, hence this post.

Looking at the docs of LabelDefinition.MaxScale and MinScale, they default to 0 which then does not limit their viewing scale. I therefore removed those assignments.

No change in results, I am afraid.

 

I made a simple repro (note to use your own API key)

https://github.com/safarviktor/ArcGIS.Net.GraphicsOverlayNotShowingLabels

ViktorSafar_1-1742977075966.png

 

0 Kudos
pnarkhede
Esri Contributor

Hi @ViktorSafar,

Thank you for providing the repro.

The issue with labels not displaying in your GraphicsOverlay appears to be related to how the label expression is defined. When using a SimpleLabelExpression with static text like "Ahoj" or "Some text", the labels won't display because the expression isn't properly linked to attributes in your graphics.

For labels to display correctly in a GraphicsOverlay, you need to:

 

1. Make sure your label expression references an attribute that exists in your graphics. Instead of:

new SimpleLabelExpression("Some text")

Try using:

new SimpleLabelExpression("[name]")

 

2. Ensure your graphics have the corresponding attribute populated:

var g = new Graphic() { Geometry = new MapPoint(0, 0) };
g.Attributes.Add("name", "Homer");
 
These changes should help your labels display correctly. Let me know If you have any further questions or need additional assistance.
 
 
Regards,
Prathamesh
0 Kudos