Skip to content

Commit 15794f9

Browse files
pjleonard37github-actions[bot]
authored andcommitted
Add tap gesture support to Marker components (#6786)
Adds tap (click) gesture support to Marker components across both iOS and Android platforms, providing API parity and consistent user interaction patterns. To keep the Markers feature simple I've only added support for Tap (Click) )gestures, but it would be trivial to add other gesture types if needed. iOS Implementation: - Add `onTapGesture` modifier to Marker component with callback support - Update SwiftUI MarkersExample to demonstrate interactive markers with color changes Android Implementation: - Add `onClick` parameter to Marker composable with optional callback function - Update MarkersActivity to showcase tap functionality with random color transitions GitOrigin-RevId: eadd595cdec6c886a7ceca08a688bfd48afb8768
1 parent 4631ef4 commit 15794f9

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Mapbox welcomes participation and contributions from everyone.
1919

2020
### Bug fixes 🐞
2121
* Fix incorrect color rendering for gradients and interpolations with zero alpha channel.
22+
* Add tap gesture support to `Marker` component with `onTapGesture` modifier
2223

2324
## 11.15.0 - 11 September, 2025
2425

Sources/Examples/SwiftUI Examples/MarkersExample.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ struct MarkersExample: View {
1717
.color(markerColor)
1818
.stroke(showStroke ? strokeColor : nil)
1919
.text(showText ? markerText : nil)
20+
.onTapGesture {
21+
markerColor = .random
22+
}
2023

2124
ForEvery(tappedPoints, id: \.latitude) { coord in
2225
Marker(coordinate: coord)
2326
.color(markerColor)
2427
.stroke(showStroke ? strokeColor : nil)
2528
.text(showText ? String(format: "%.3f, %.3f", coord.latitude, coord.longitude) : nil)
29+
.onTapGesture {
30+
markerColor = .random
31+
}
2632
}
2733

2834
TapInteraction { tapContext in

Sources/MapboxMaps/Annotations/Marker.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import SwiftUI
22

3-
/// Displays a simple map Marker at the specified coordinated.
3+
/// Displays a simple map Marker at the specified coordinate.
44
///
55
/// `Marker` is a convenience struct which creates a simple `MapViewAnnotation` with limited customization options.
66
/// Use `Marker` to quickly add a pin annotation at the specific coordinates when using SwiftUI.
@@ -19,6 +19,7 @@ import SwiftUI
1919
/// - Note: `Marker`s are great for displaying unique interactive features. However, they may be suboptimal for large amounts of data and don't support clustering.
2020
/// Each marker creates a SwiftUI view, so for scenarios with 100+ markers, consider using ``PointAnnotation``,
2121
/// Additionally, `Marker`s appear above all content of MapView (e.g. layers, annotations, puck). If you need to display annotation between layers or below a puck, use ``PointAnnotation``.
22+
@_documentation(visibility: public)
2223
@_spi(Experimental)
2324
public struct Marker {
2425

@@ -42,6 +43,9 @@ public struct Marker {
4243
/// The color of optional strokes
4344
var strokeColor: Color? = Color(red: 58/255, green: 89/255, blue: 250/255, opacity: 1.0)
4445

46+
/// The tap action to perform when marker is tapped
47+
var tapAction: (() -> Void)?
48+
4549
/// The outer image of the Marker
4650
private let outerImage = Image("default_marker_outer", bundle: .mapboxMaps)
4751

@@ -74,6 +78,11 @@ public struct Marker {
7478
with(self, setter(\.strokeColor, color))
7579
}
7680

81+
/// Set a tap action for the Marker
82+
public func onTapGesture(perform action: @escaping () -> Void) -> Self {
83+
with(self, setter(\.tapAction, action))
84+
}
85+
7786
/// Build a `MapViewAnnotation` with the current Marker properties
7887
private func build() -> MapViewAnnotation {
7988
MapViewAnnotation(coordinate: coordinate) {
@@ -83,6 +92,9 @@ public struct Marker {
8392
markerText(text)
8493
}
8594
}
95+
.onTapGesture {
96+
tapAction?()
97+
}
8698
}
8799
.allowOverlap(true)
88100
}

Sources/MapboxMaps/Documentation.docc/Documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The Mapbox Maps SDK for iOS is a public library for displaying interactive, thor
3232

3333

3434
### Annotations
35+
- ``Marker``
3536
- <doc:Layer-Annotations>
3637
- <doc:View-Annotations>
3738

Tests/MapboxMapsTests/Annotations/ViewAnnotationTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ final class ViewAnnotationTests: XCTestCase {
239239
XCTAssertEqual(marker.strokeColor, Color(red: 58/255, green: 89/255, blue: 250/255, opacity: 1.0))
240240
XCTAssertEqual(marker.coordinate, CLLocationCoordinate2D.testConstantValue())
241241
XCTAssertEqual(marker.text, nil)
242+
XCTAssertNil(marker.tapAction)
243+
}
244+
245+
func testMarkerTapGesture() {
246+
var tapped = false
247+
let marker = Marker(coordinate: .testConstantValue())
248+
.text("Test Marker")
249+
.onTapGesture {
250+
tapped = true
251+
}
252+
253+
XCTAssertNotNil(marker.tapAction)
254+
XCTAssertFalse(tapped)
255+
256+
// Execute the tap action
257+
marker.tapAction?()
258+
XCTAssertTrue(tapped)
242259
}
243260
}
244261

scripts/api-compatibility-check/breakage_allowlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,3 +2193,6 @@ Constructor MapSnapshotOptions.init(size:pixelRatio:glyphsRasterizationOptions:s
21932193

21942194
// Add scale bar nautical units
21952195
Constructor ScaleBarViewOptions.init(position:margins:visibility:useMetricUnits:) has been removed
2196+
2197+
// Documentation for Marker
2198+
Struct Marker is now with @_documentation

0 commit comments

Comments
 (0)