swiftmapkit

MapAnnotation create generic parameter issues


I'm getting the error "Generic parameter 'V' could not be inferred" when adding MapAnnotation in the ForEach. My objective is to have clickable marker, that's why I'm adding the annotation.

Example of code:

@State private var sessions: [Session] = []

var body: some View {
   Map {
      ForEach(sessions) { session in
         MapAnnotation(coordinate: session.getCoordinate()) {}
      }
   }
}

Example of Session object:

import Foundation
import MapKit

struct Session : Identifiable, Codable {

   var SessionId: Int
   var lat: Double
   var lng: Double

   var id: Int {
      SessionId
   }

   func getCoordinate() -> CLLocationCoordinate2D {
      return CLLocationCoordinate2D(latitude: lat, longitude: lng)
   }
}

I tried:

How can I fix it?


Solution

  • MapAnnotation is deprecated and replaced by Annotation

    var body: some View {
        Map {
            ForEach(sessions) { session in
                Annotation("", coordinate: session.getCoordinate()) {
                    Text("\(session.SessionId)")
                }
            }
        }
    }