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:
ForEach(session, id: \.id)
and lot of others variants of "id:"annotationItems
which seems to no longer be availableHow can I fix it?
MapAnnotation is deprecated and replaced by Annotation
var body: some View {
Map {
ForEach(sessions) { session in
Annotation("", coordinate: session.getCoordinate()) {
Text("\(session.SessionId)")
}
}
}
}