Interactions API
The new Interactions API is a toolset that allows you to add interactions on layers, predefined featuresets in evolving basemap styles like Standard, and the map itself. The API is available starting from Flutter Maps SDK v2.6.0
.
The Interactions API is an experimental feature of the Flutter Maps SDK. It is subject to breaking changes in minor versions.
To use the API, you define and add TapInteraction
or LongTapInteraction
interaction handlers that target specific map layers, featuresets, or the map itself. When a user interacts with map features belonging to one of these featuresets, the API will call the appropriate interaction handler for that feature that was tapped or long-tapped.
Add an interaction to a map layer
Add interactions to the map by indicating the layer that should handle the interaction. The interaction will be triggered when a user taps or long-taps on a feature rendered on the specified layer. To add an interaction, use mapboxMap.addInteraction()
:
mapboxMap.addInteraction(TapInteraction(FeaturesetDescriptor(layerId: "polygons"), (feature, context) {
// Handle tap when a feature from "polygons" is tapped.
}), interactionID: "polygonTapInteraction");
// Remove the interaction by passing the interaction ID to .removeInteraction().
mapboxMap.removeInteraction("polygonTapInteraction");
The interaction handler (TapInteraction
) in the example above will be called each time a user taps a feature rendered on the polygons
layer. The handler receives the feature that was tapped and the context containing information about the event. You can add an interaction at any time, there is no need to wait for the style to load. If there is no layer in the style with the name provided, then no interaction will be added.
Add an interaction with featuresets
Interactions can be added not only to a layer, but also to a featureset. Featuresets are named groups of layers that can be defined in an evolving basemap style. In the Mapbox Standard Style, there are predefined Points-of-Interest (POI), Place Labels, and Buildings featuresets that include all appropriate features in the map. You can add interactions to your map that target these featuresets. Use the predefined standardPoi
, standardPlaceLabels
, and standardBuildings
featureset descriptors to enable interactions in the Standard Style.
mapboxMap.addInteraction(TapInteraction(StandardPOIs(), (poi, _) {
log("Tap at poi ${poi.name}");
});
When you use a predefined featureset, such as StandardPOIs()
, the interaction will return a feature of a matching type (here a StandardPoiFeature
). This means you will have access to properties specific to that feature type, such as name
in the example above. To learn more about typed features that match the predefined featuresets, read the StandardPoiFeature
, StandardPlaceLabelsFeature
, and StandardBuildingsFeature
documentation.
Set feature states
After a feature is returned from the interaction you can set its feature state. Setting the feature state allows you to control the appearance of individual features within a featureset.
For example, you may want to highlight individual buildings after a user taps on them. To do this, you would add a TapInteraction
targeting the StandardBuildings()
featureset. When a user taps on a building in this featureset, the building feature is returned as a StandardBuildingsFeature
. You then set the feature state for this feature's highlight
configuration option to true
. By default, selected buildings will be displayed in blue, as shown in the image below. You can customize the color of selected buildings.
var tapInteraction = TapInteraction(StandardBuildings(), (building, _) {
mapboxMap.setFeatureStateForFeaturesetFeature(building, StandardBuildingState(highlight: true));
});
mapboxMap.addInteraction(tapInteraction);
Each predefined featureset in the Standard Style has appropriate configuration options that can be modified at runtime in this way. Explore the StandardPoiState
, StandardPlaceLabelsState
, and StandardBuildingsState
documentation to learn more about the specific configuration options available for each featureset.
Add an interaction to the map itself
You can add a special variant of the Tap
and LongTap
interactions that doesn't take any layer or featureset as an argument. This lets you handle taps on the "map itself". This usually happens when user clicks a place on the map where there are no interactive features or when the event propagated down to the map. context
contains information about the interaction event, such as the point where the tap occurred.
var tapInteraction = TapInteraction.onMap((context) {
log("Tap on map itself at: ${context.point.coordinates.lat}, ${context.point.coordinates.lng}");
});
mapboxMap.addInteraction(tapInteraction);
/// or
mapboxMap.addInteraction(LongTapInteraction.onMap((context) {
log("LongTap on map itself at: ${context.point.coordinates.lat}, ${context.point.coordinates.lng}");
}));
Interactions order and propagation
In the map, there might be many features handling interactions. If a user taps at the point where multiple features are rendered, each of them can have a chance to handle the interaction. The Maps SDK will invoke each interaction handler (TapInteraction
or LongTapInteraction
) one by one in the order that features are rendered. The top-most feature will be invoked first. So, if there are multiple interactions added to the same target (such as a layer and a featureset), the interactions added last will be called first.
By default, each interaction handler will completely handle the user interaction and stop propagation. If you want to continue propagating the interaction after the particular interaction handler is triggered, then set the stopPropagation
parameter to false
on the interaction.
Examples
To learn more about working with the Interactions API, explore our example using this API:
Dart example that demonstrates how to add interactions to predefined featuresets in the Mapbox Standard Style.