Show feature labels on featureLayer doesn't work

710
2
01-22-2018 04:26 AM
MarcWouters
New Contributor III

I followed


But my application fails to show the labels of the features.

I have no idea what I'm what I'm doing wrong.

My code is 

FeatureLayer* iconLayer = new FeatureLayer(connectionsTable, _map);
QString labelDefJSON = "{";
labelDefJSON += "\"labelPlacement\" : \"esriServerPointLabelPlacementAboveRight\",";
labelDefJSON += "\"labelExpressionInfo\" :{";
labelDefJSON += "\"expression\" : \"$feature.Name\" },";
labelDefJSON += "\"symbol\" : {";
labelDefJSON += "\"type\": \"esriTS\",";
labelDefJSON += "\"font\" : { \"family\": \"Arial\", \"size\" : 11,\"weight\" : \"bold\"}";
labelDefJSON += "}";
labelDefJSON += "}";

 LabelDefinition* labelDef = LabelDefinition::fromJson(labelDefJSON);
 iconLayer->labelDefinitions()->append(labelDef);

 iconLayer->setLabelsEnabled(true);

_map->operationalLayers()->append(iconLayer);

Before appending the layer to the operationalLayers(), I also set a renderer for the features themselves (icons). Could this interfere with showing the labels ?

The JSON string seems to be valid (according to VisualStudio's JSON Visualizer).

Searching the Internet, I've found some alternatives :

  • start the JSON string with "labelingInfo" 
  • use "expression" : "return $feature.Name;"
  •  "value: "$feature.Name"

Nothing seems to help.

Should I maybe do something different with "setLabelsEnabled(true)" ?

Any help is welcome.

Regards,

Marc

Tags (2)
0 Kudos
2 Replies
LucasDanzinger
Esri Frequent Contributor

I tried your code with a sample service (and modifying the field name), and it works for me. Can you give it a try? If that works, does your expression accurately reflect the name of your field, and not an alias or something?

  // create the layer
  ServiceFeatureTable* ft = new ServiceFeatureTable(QUrl("http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"), this);
  FeatureLayer* fl = new FeatureLayer(ft, this);

  // create the label definition
  QString labelDefJSON = "{";
  labelDefJSON += "\"labelPlacement\" : \"esriServerPointLabelPlacementAboveRight\",";
  labelDefJSON += "\"labelExpressionInfo\" :{";
  labelDefJSON += "\"expression\" : \"$feature.typdamage\" },";
  labelDefJSON += "\"symbol\" : {";
  labelDefJSON += "\"type\": \"esriTS\",";
  labelDefJSON += "\"font\" : { \"family\": \"Arial\", \"size\" : 11,\"weight\" : \"bold\"}";
  labelDefJSON += "}";
  labelDefJSON += "}";

  LabelDefinition* labelDefinition = LabelDefinition::fromJson(labelDefJSON, this);

  fl->setLabelsEnabled(true);
  fl->labelDefinitions()->append(labelDefinition);
  m_map->operationalLayers()->append(fl);

  // Set map to map view
  m_mapView->setMap(m_map);
0 Kudos
MarcWouters
New Contributor III

Hi Lucas,

I've checked again to be sure : field name and alias are both 'Name'.

I've checked both for the layer and the mapView that isLabelsEnabled() return true.

I've ran the code without the attached renderer.

I've changed the parent for the LabelDefinition to this (was empty) and the parent for the layer also to this (was the map).

Nothing.

The only difference I can see now is that my FeatureLayer is based on a GeodatabaseFeatureTable, while you use a ServiceFeatureTable in your sample code. 

Could this be the reason why I don't see labels ?

Looking further in the reference I noticed a class LabelingInfo. But I cannot find how to connect this to the layer of the mapView. Could this help ?

Thanks for looking into this further.

Marc

0 Kudos