I'm implementing a Map with user location customization in SwiftUI using iOS 17+ MapKit APIs. When using the selection
parameter with Map,
the default blue dot user location becomes tappable but shows an empty annotation view. However, using UserAnnotation
makes the location marker non-interactive.
My code structure:
import SwiftUI
import MapKit
struct UserAnnotationSample: View {
@State private var position: MapCameraPosition = .userLocation(fallback: .automatic)
@State private var selectedItem: MapSelection<MKMapItem>?
var body: some View {
Map(position: $position, selection: $selectedItem) {
// UserAnnotation()
}
.mapControls {
MapUserLocationButton()
}
}
}
Key questions:
- How can I replace the empty annotation view with a custom avatar when tapping the user location?
- Is there a way to make UserAnnotation interactive with selection?
- Should I use tag modifier for custom annotations? What's the proper way to associate selections?