Skip to main content

Interactions API

The new Interactions API is a toolset that allows you to handle interactions on layers, predefined featuresets in evolving basemap styles like Standard, and the map itself. The API is available starting from Maps SDK v11.9.0.

Note

The Interactions API is an experimental feature of the iOS Maps SDK. To build with it use the following import statement:
@_spi(Experimental) import MapboxMap

To use the API, you define and add TapInteraction or LongPressInteraction 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 clicked or long-pressed.

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-presses on a feature rendered on the specified layer. In SwiftUI, place the interactions inside the Map using TapInteraction or LongPressInteraction and in UIKit applications, use MapboxMap.addInteraction() to add the interaction:


Map {
TapInteraction(.layer("polygons")) { feature, context in
// Handle tap when a feature from "polygons" is clicked.
return true // stops propagation
}
}

The interaction handler (TapInteraction) in the examples 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 the interaction should be active as long as the MapView is alive, then the cancellation token can be omitted. 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.


Map {
TapInteraction(.standardPoi) { poi, context in
print("Tap at poi: ", poi.name ?? "")
return true
}
}

When you use a predefined featureset, such as .standardPoi 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 select configuration option to true. By default, selected buildings will be displayed in blue, like in the image below. You can customize the color of selected buildings.


Map {
@State var selectedBuildings = [StandardBuildingsFeature]()
TapInteraction(.standardBuildings) { building, _ in
self.selectedBuildings.append(building)
return true
}

// Append this feature to the array of selected buildings to update the featurestate
ForEvery(selectedBuildings, id: .id) { building in
FeatureState(building, .init(select: true))
}
}

Each predefined featureset in the Standard Style has appropriate configuration options that can be modified at runtime in this way. Explore the StandardPoiFeature, StandardPlaceLabelsFeature, and StandardBuildingsFeature 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 LongPress 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.


Map {
TapInteraction { context in
print("Tap on map itself at (context.coordinate)")
return true
}

LongPressInteraction { context in
print("Long press on map itself at (context.coordinate)")
return true
}
}

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 LongPressInteraction) 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 return false in the callback.

Examples

To learn more about working with the Interactions API, explore our examples using this API:

EXAMPLE
Add interactions to predefined featuresets in Mapbox Standard

SwiftUI example that demonstrates how to add interactions to predefined featuresets in the Mapbox Standard Style.

EXAMPLE
Add interactions to predefined featuresets in Mapbox Standard

UI Kit example that demonstrates how to add interactions to predefined featuresets in the Mapbox Standard Style.

EXAMPLE
Add interactions to custom featuresets and the map

SwiftUI example that demonstrates how to add interactions to custom featuresets and the map itself.

EXAMPLE
Add interactions to custom featuresets and the map

UI Kit example that demonstrates how to add interactions to custom featuresets and the map itself.

Was this page helpful?