Working with selection
The Felt SDK provides functionality for reading the current selection state and selecting features on the map programmatically. This is useful for building interactive experiences that respond to data analysis or user interactions.
Selecting Features
Features are individual data points within layers. You can select features programmatically using the selectFeature() method. Only one feature can be selected at a time - selecting a new feature will replace the current selection.
await felt.selectFeature({
id: "feature-123",
layerId: "buildings-layer"
});The selectFeature() method accepts options to control the selection behavior:
await felt.selectFeature({
id: "feature-123",
layerId: "buildings-layer",
showPopup: false, // Whether to show the feature popup (default: true)
fitViewport: { maxZoom: 15 } // Fit viewport to feature with zoom limit
});showPopup(boolean, default:true) - Whether to display the feature information popupfitViewport(boolean |{ maxZoom: number }, default:true) - Whether to fit the viewport to the feature. Can betrue,false, or an object withmaxZoomto limit the zoom level used.
Reading selection
You can get the current selection state using getSelection(). This returns an array of EntityNode objects, each representing a selected entity. The selection can include various types of entities at the same time, such as annotations and features:
Clearing Selection
Remove current selections using clearSelection():
Reacting to selection changes
To stay in sync with selection changes, use the onSelectionChange() method:
Best practices
Clean up listeners: Always store and call the unsubscribe function when you no longer need to listen for selection changes:
Handle empty selection: Remember that the selection array might be empty if nothing is selected:
By following these patterns, you can build robust interactions based on what users select in your Felt map.
Last updated
Was this helpful?