Map: onTapGesture not working

The onTapGesture is not working inside the ViewMapAnnotation, here is my code:

 `Map(
        coordinateRegion: $region,
        type: .standard,
        pointOfInterestFilter: .includingAll,
        interactionModes: [.all],
        annotationItems: evChargerManager.evChargers,
        annotationContent: { charger in

            ViewMapAnnotation(coordinate: (charger.addressInfo?.coordinates)!) {

                Image(K.Images.SupplementarilyIcons.pinpoint)
                    .resizable()
                    .frame(width: 40, height: 40, alignment: .center)
                    .onTapGesture {
                        showingSheet.toggle()
                    }
                    .sheet(isPresented: $showingSheet) {
                        EVChargerView(evCharger: charger)
                    } //: SHEET

            } //: VIEW MAP ANNOTATION

        } //: ANNOTATION CONTENT

    ) //: MAP`

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 23 (4 by maintainers)

Most upvoted comments

@IraklisElef As I thought, the problem is not connected with the Map library. To show the sheet correctly, try the following code:

struct YourView: View {
  ...

  @State private var selectedAnnotation: Annotation? = nil

  var body: some View {
      Map(
          coordinateRegion: $region,
          annotationItems: annotations,
          annotationContent: { annotation in
              ViewMapAnnotation(coordinate: annotation.coordinate) {
                 Image(K.Images.SupplementarilyIcons.pinpoint)
                  .resizable()
                  .frame(width: 40, height: 40, alignment: .center)
                  .onTapGesture {
                      selectedAnnotation = annotation
                  }
              }
          }
      )
      .sheet(item: $selectedAnnotation) { annotation in
          List {
              Text("Sheet! \(annotation.id.uuidString)")
          }
      }
   }
}