Show labels for graphics

959
3
05-31-2019 06:17 AM
AlexMeleshko
New Contributor

Hello,

I want to show individual labels for each object on my map. For example, I have map with many moving planes (use PictureMarkerSymbol), and I want to show their flight numbers.

I've tried to use same methods, as in this example, but it didn’t work.

https://developers.arcgis.com/qt/latest/cpp/sample-code/sample-qt-showlabelsonlayers.htm 

 

const QString labelJson(QStringLiteral(
"{\"labelExpressionInfo\":{\"expression\":\"# number\"},\"labelPlacement\":\"esriServerPointLabelPlacementAboveRight\",\"symbol\": {\"color\":[0,0,255,255],\"font\": {\"size\":15}"));
LabelDefinition* labelDef = LabelDefinition::fromJson(labelJson, this);

m_graphicsOverlay->labelDefinitions()->append(labelDef);
m_graphicsOverlay->setLabelsEnabled(true);

 

Is possible to use labelDefinitions for my task? How I need to write "expression":"# number" - to show individual  labels for each graphics?

 

Regards,

Alex

0 Kudos
3 Replies
LucasDanzinger
Esri Frequent Contributor

What is showing up in your example currently? I think if you restructured your expression to use brackets like show in the docs, it might work:

assuming there is an attribute named "number" in the graphic:

 "expression":"# [number]"

doc - https://developers.arcgis.com/documentation/common-data-types/labeling-objects.htm

0 Kudos
AlexMeleshko
New Contributor

It doesn't work, no with brackets or without them.

Here is a small example:

https://drive.google.com/open?id=1nx_9GUXfzU-XInE_x2Mhow88OKgM4-wB 

0 Kudos
LucasDanzinger
Esri Frequent Contributor

Please see the below code. Your code was pretty close, but the graphic needs to have an attribute added with the desired "number". I reformatted the expression a little. Feel free to further tweak it to meet your needs.

    // #1
    SimpleMarkerSymbol* symbol_1 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("blue"), 10, this);
    Point point_1(10.5, 40.5, SpatialReference(4236));
    Graphic* g1 = new Graphic(point_1, symbol_1);
    // insert attribute called "number" with desired value
    g1->attributes()->insertAttribute("number", 1); // <---- THIS IS THE KEY ADDITION
    m_graphicsOverlay->setRenderer(new SimpleRenderer(symbol_1, this));
    m_graphicsOverlay->graphics()->append(g1);

    // #2
    SimpleMarkerSymbol* symbol_2 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("red"), 10, this);
    Point point_2(10.5, 40, SpatialReference(4236));
    Graphic* g2 = new Graphic(point_2, symbol_2);
    g2->attributes()->insertAttribute("number", 2);
    m_graphicsOverlay->setRenderer(new SimpleRenderer(symbol_2, this));
    m_graphicsOverlay->graphics()->append(g2);

    // #3
    SimpleMarkerSymbol* symbol_3 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("green"), 10, this);
    Point point_3(10, 40.5, SpatialReference(4236));
    Graphic* g3 = new Graphic(point_3, symbol_3);
    g3->attributes()->insertAttribute("number", 3);
    m_graphicsOverlay->setRenderer(new SimpleRenderer(symbol_3, this));
    m_graphicsOverlay->graphics()->append(g3);

    //Labeling - label content is driven off the "number" attribute in each graphic. 
    // MAKE SURE TO UPDATE THE EXPRESSION TO USE THIS SYNTAX
    const QString labelJson(R"({"labelExpressionInfo":{"expression":"$feature.number"},"labelPlacement":"esriServerLinePlacementAboveAlong","symbol":{"angle":0,"backgroundColor":[0,0,0,0],"borderLineColor":[0,0,0,0],"borderLineSize":0,"color":[0,0,255,255],"font":{"decoration":"none","size":11.25,"style":"normal","weight":"normal"},"haloColor":[0,0,0,0],"haloSize":0,"horizontalAlignment":"center","kerning":false,"type":"esriTS","verticalAlignment":"middle","xoffset":0,"yoffset":0}})");
    LabelDefinition* labelDef = LabelDefinition::fromJson(labelJson, this);
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍