MapAnnotation
A customizable annotation that marks a map location.
Declaration
struct MapAnnotation<Content> where Content : ViewOverview
Use MapAnnotation to declare the layout of the view that MapKit uses for the annotation. Create a Map and display annotations by returning a view that conforms to MapAnnotationProtocol from the trailing closure of init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:) or init(mapRect:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:). Items you provide as a collection to the source annotations need to conform to Identifiable.
For example, the following code displays a map and a single annotation:
struct IdentifiablePlace: Identifiable {
let id: UUID
let location: CLLocationCoordinate2D
init(id: UUID = UUID(), lat: Double, long: Double) {
self.id = id
self.location = CLLocationCoordinate2D(
latitude: lat,
longitude: long)
}
}
struct CustomAnnotationMapView: View {
let place: IdentifiablePlace
@State var region: MKCoordinateRegion
var body: some View {
Map(coordinateRegion: $region,
annotationItems: [place]
) { place in
MapAnnotation(coordinate: place.location) {
Rectangle().stroke(Color.blue)
.frame(width: 20, height: 20)
}
}
}
}