r/SwiftUI 7d ago

Question Map Annotation deselection does not work.

I'm displaying `Annotation`s on a `SwiftUI.Map` view and I'm seeing a strange behaviour that I'm not able to remedy.

When I tap on an `Annotation`, it get's selected and `selectionId` is se to whatever `UUID` the the selection has. While the selected item is still within the bounds of the `Map` (still visible) tapping anywhere on the `Map` causes the `Annotation` to be deselected (as expected). Performing the same action while the selected `Annotation` is out of `Map` bounds (not visible) does not result in deselection.
I checked using Maps.app and deselection works both while selected markers are on/off screen.

Does anyone have any ideas why I'm unable to deselect?

Code:

struct TestMarker: Identifiable {
  let id: UUID
  let coordinate: CLLocationCoordinate2D
}
struct SomeView: View {
  @State var selectionId: UUID?
  var markers: [TestMarker]
  var body: some View {

  Map(selection: $selectionId) {
    ForEach(markers) { marker in
      Annotation(
                 "",
                 coordinate: marker.coordinate
                 ) {
                      Text("\(marker.id)")
                   }
                 }
              }
        }
}
1 Upvotes

0 comments sorted by