'SwiftUI - Custom map pin's onTapGesture action is selecting a wrong pin view when there are group of pins in the save position(Zindex bug)

My map code is actually really simple:

//
//  ContentView.swift
//  MapTest
//
//  Created by Asi Givati on 24/04/2022.
//

import SwiftUI
import MapKit
import Combine

class ItemsManager: NSObject, ObservableObject {
    static let shared = ItemsManager()
    var items:[Item] = [.init(title:"Pin1"), .init(title:"Pin2"), .init(title:"Pin3"), .init(title:"Pin4"), .init(title:"Pin5"), .init(title:"Pin6"), .init(title:"Pin7")]
    @Published var regionForMap = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 32.0525500, longitude: 34.80285150000003),
        span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
}

class Item:ObservableObject, Codable, Identifiable {
    var title:String
    private let latitudeVal:Double
    private let longitudeVal:Double
    var coordinate:CLLocationCoordinate2D { .init(latitude: latitudeVal, longitude: longitudeVal) }
    var id = UUID()
    init(title:String) {
        self.title = title
        self.latitudeVal = Double.random(in: 32.0525500...32.0847500)
        self.longitudeVal = Double.random(in: 34.80285150000003...34.814851700010030)
    }
}

struct MyMapScreen: View {
    @ObservedObject private var manager = ItemsManager.shared
    @State private var items = [Item]()
    @State private var selectedItemId:UUID?
    var body: some View {
        ZStack {
            Map(coordinateRegion: $manager.regionForMap, interactionModes:.all, showsUserLocation: true, userTrackingMode: .constant(.none), annotationItems:ItemsManager.shared.items) { item in
                MapAnnotation(coordinate: item.coordinate, anchorPoint: .zero) {
                    CustomMapPin(item: item, isSelected:selectedItemId == item.id)
                        .border(.red)
                        .onTapGesture {
                            selectedItemId = item.id
                            // do something with 'item'
                        }
                }
            }
        }
    }
}

private struct CustomMapPin:View {
    var item:Item
    var isSelected:Bool
    
    var body: some View {
        Text(item.title)
            .frame(width: 250, height: 100, alignment: .center)
            .background(isSelected ? Color.green : Color.blue)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        MyMapScreen()
    }
}

i need to do some action on the onTapGesture action, but the selected item is wrong when there are other pins below it. it's like the tap action is going through the pin and click on the one that blow it.

when the view looks like that it's all good:

enter image description here

but when zooming in the issue occurs (click on the green pin - is execute the onTapGesture action of the bottom pin):

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source