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:
-
Create a
FeatureLayer
based on the model and styling information (IFeaturePainter
) -
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:
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:
-
IFeaturePainter::configureMetadata
: used to retrieve metadata about the painter. This metadata is used by the layer to determine when a feature needs to be re-processed by the painter. For example, a feature needs to be painted differently after a certain property of the feature has changed. For more information, see the documentation ofFeaturePainterMetadata
. -
IFeaturePainter::paint
: used to determine how to visualize a feature. Given aFeature
andcontext
, this method performsdraw
calls on theFeatureCanvas
parameter.
This code snippet shows an implementation of the
IFeaturePainter::paint
method:
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:
-
Fill Style : describes the style of a surface, a fill color or specific textures for example.
-
Line Style : describes the style of a line, a line color or width for example.
-
Complex Stroke Line Style : describes the style of a line with a complex stroke pattern, such as a dashed line.
Drawing icons and text
You can draw icons and text on the map with FeatureCanvas::drawIcon
and
FeatureCanvas::drawText
respectively.
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.