Adding a feature model

You can add and remove data on the Map using Map::getLayerList().

To add a feature model to the map, you:

  1. Create a FeatureLayer based on the model and styling information (IFeaturePainter)

  2. Add that layer to the Map’s LayerList

This code snippet demonstrates how a feature model is added to the map, configured with a custom painter:

Program: Adding a feature layer
std::shared_ptr<IFeatureModel> geometryModel = CustomerGeometryModel::create();
std::shared_ptr<FeatureLayer> geometryLayer = FeatureLayer::newBuilder()
.model(geometryModel)
.editable(true)
.editCreateGeometryProvider(CustomerGeometryModel::createEditGeometryProvider())
.build();
map->getLayerList()->add(geometryLayer);

Styling features

Apart from a mandatory IFeatureModel parameter on its constructor, the FeatureLayer::Builder also accepts an optional IFeaturePainter, which you can use to set up custom styling of the model features.

Creating your own IFeaturePainter requires an implementation of two methods:

This code snippet shows an implementation of the IFeaturePainter::paint method:

Program: Using the feature painter
void RoadsPainter::paint(const Feature& feature, const FeaturePainterContext& context, FeatureCanvas& canvas) const {
const std::optional<std::shared_ptr<Geometry>>& geometry = feature.findGeometry();
if (!geometry) {
std::cerr << "Geometry not found for the given feature: " << feature.getId() << std::endl;
return;
}
std::string roadType = feature.getValue<std::string>(_roadTypePropertyPath).value_or("unknown");
size_t detailLevel = context.getDetailLevel();
std::map<StyleKey, LineStyle> styleMap;
std::map<StyleKey, TextStyle> labelStyleMap;
if (context.isFeatureStateEnabled(FeatureState::selected())) {
styleMap = _selectedStyles;
labelStyleMap = _selectedLabelStyles;
} else if (context.isFeatureStateEnabled(FeatureState::hover())) {
styleMap = _hoverStyles;
labelStyleMap = _hoverLabelStyles;
} else {
styleMap = _regularStyles;
labelStyleMap = _regularLabelStyles;
}
StyleKey key = std::make_pair(detailLevel, roadType);
auto iterator = styleMap.find(key);
LineStyle style = iterator != styleMap.end() ? iterator->second : _fallbackStyle;
canvas.drawGeometry().geometry(*geometry).stroke(style).submit();
auto labelIterator = labelStyleMap.find(key);
bool foundLabel = labelIterator != labelStyleMap.end();
std::optional<std::string> name = feature.getValue<std::string>(_namePropertyPath);
if (foundLabel && name) {
TextStyle labelStyle = labelIterator->second;
canvas.drawLabel().anchor(*geometry).textStyle(labelStyle).text(*name).submit();
}
}

Visualizing geometries

You can visualize geometries with the FeatureCanvas::drawGeometry method. This method requires at least a Geometry and a stroke and/or fill style.

You create Feature styles through builder methods on one of the style classes. The different types of feature styles are:

Adding labels to features

You can attach icon and text labels to features with the FeatureCanvas::drawLabel method. See related article on adding labels for more information.