I’m trying some basic tutorials for testing MapKit. I have added two custom annotations and the map automatically zooms out to show both annotation coordinates, but the annotations views are cut on the left and right margins. Is there a way to define a zoom level to initially show the full annotations views?
The code to replicate the issue:
import SwiftUI
import MapKit
struct Pin: Identifiable {
let id = UUID()
let name: String
let coordinates: CLLocationCoordinate2D
}
struct ProjectMapView: View {
let locations: [Pin] = [
.init(name: "New York", coordinates: CLLocationCoordinate2D(latitude: 40.712548889567415, longitude: -74.00637733127851)),
.init(name: "Rome", coordinates: CLLocationCoordinate2D(latitude: 41.89682093385061, longitude: 12.482471453457123)),
]
var body: some View {
VStack {
Map {
ForEach(locations) { location in
Annotation(location.name, coordinate: location.coordinates) {
Color(.systemPink)
.frame(width: 50, height: 50)
.clipShape(Circle())
.overlay { Circle().stroke(.white, lineWidth: 2) }
.shadow(radius: 4)
}
}
}
.mapStyle(.hybrid(elevation: .flat))
}
.ignoresSafeArea()
}
}
#Preview {
ProjectMapView()
}
The result: