Search code examples
swiftuimapkitmapkitannotation

How to make a view conform to MapAnnotationProtocol


I have a tappable MapAnnotation and whenever I try to customize the "look" of the annotation pin, I get the following error:

Initializer 'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' requires that 'NavigationLink<PlaceAnnotationView, LocationDetailsView>' conform to 'MapAnnotationProtocol'

Annotation is defined by this:

struct Annotation: Codable, Identifiable, Hashable {
    @DocumentID var id: String?
    let lat: String?
    let lng: String?
    var name: String
}

And the Map is defined by this:

Map(coordinateRegion: $region, annotationItems: annotations) { place in
    NavigationLink {
        LocationDetailsView(place: place.name)
    } label: {
        PlaceAnnotationView(title: place.name)
    }
}

Have I forgotten to define something? Please let me know. Thanks.


Solution

  • https://developer.apple.com/documentation/mapkit/mapannotationprotocol

    Don’t create types conforming to MapAnnotationProtocol protocol. Instead, use one of the framework-provided types MapAnnotation, MapMarker, and MapPin.

    https://developer.apple.com/documentation/mapkit/mapannotation