MapView를 사용하기 위해선 지도에 표시되는 좌표를 추적할 수 있는 State 가 있어야 한다. MKCoordinateRegion 을 사용해야 하고, latitude / longitude 에 좌표를 입력해주면 원하는 위치를 MapView 가 실행될 때 표시된다.
기본 코드
import SwiftUI
import MapKit
struct ContentView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.5666791, longitude: 126.9782914), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
var body: some View {
Map(coordinateRegion: $region)
}
}
실행 화면
확대/축소 및 이동 금지
interactionModes 매개변수를 제공하여 사용자가 지도를 제어할 수 있는 정도를 제한할 수 있다.
Map(coordinateRegion: $region, interactionModes: [.zoom])
확대/축소 및 이동 가능
showsUserLocation 과 userTrackingMode 매개 변수를 사용해 지도 이동 및 확대/축소가 가능하게 한다.
Map(coordinateRegion: $region, showUserLocation: true, userTrackingMode: .constant(.follow))
개발자 Document
https://developer.apple.com/documentation/mapkit/map
Apple Developer Documentation
developer.apple.com
'Language > Swift' 카테고리의 다른 글
SwiftUI 를 이용한 이미지 또는 텍스트에 마스크 씌우기 (Mask) (0) | 2022.12.22 |
---|---|
SwiftUI를 이용한 메뉴 그리기 (Menu) (0) | 2022.12.21 |
SwiftUI 에서 Path 사용하기 (0) | 2022.12.15 |
SwiftUI 에서 반복문 사용하기 (ForEach) (0) | 2022.12.14 |
SwiftUI를 이용한 색상 선택 Picker 이용하기 (ColorPicker) (0) | 2022.12.12 |
댓글