import SwiftUI
import MapKit
struct SimpleMapScreen: View {
private let nycCoordinate = CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060)
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060),
span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
)
var body: some View {
VStack {
Text("Simple Map Screen")
.font(.headline)
.padding()
Map(coordinateRegion: $region, annotationItems: [AnnotationItem(coordinate: nycCoordinate)]) { item in
MapMarker(coordinate: item.coordinate, tint: .red)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
}
struct AnnotationItem: Identifiable {
let coordinate: CLLocationCoordinate2D
var id: String { "\(coordinate.latitude),\(coordinate.longitude)" }
}
struct SimpleMapScreen_Previews: PreviewProvider {
static var previews: some View {
SimpleMapScreen()
}
}We use @State to hold the map region centered on New York City with a small zoom span. The Map view binds to this region so the user can zoom and pan. We create an AnnotationItem struct to hold the pin location and pass it to the Map view's annotationItems parameter. The MapMarker displays a red pin at the center coordinate. The map fills the screen below the title text.