A solution is to create a struct that signs MapAnnotationProtocol with some Any type attributes. Then call MapAnnotation with that struct. So onTapGesture works.
1) Create a struct that conforms to MapAnnotationProtocol.
struct AnyMapAnnotationProtocol: MapAnnotationProtocol {
let _annotationData: _MapAnnotationData
let value: Any
init<WrappedType: MapAnnotationProtocol>(_ value: WrappedType) {
self.value = value
_annotationData = value._annotationData
}
}
2) Call the new SwiftUI Map View's closure, then call the annotation previously set.
Map(coordinateRegion: $region, interactionModes: [.all], showsUserLocation: false, userTrackingMode: .constant(.follow), annotationItems: annotatedStations) { item in
AnyMapAnnotationProtocol(MapAnnotation(coordinate: itemCoordinate) {
HStack {
Image("byeByeDidSelect")
.resizable()
.frame(width: 45, height: 40)
.clipShape(Capsule())
Text("This annotation can be tapped.")
.foregroundColor(.black)
}
.onTapGesture {
print("Test tapping")
}
})
}
Hope help you guys.