본문 바로가기
Language/Swift

SwiftUI를 이용한 지도 그리기(MapView)

by IFLA 2022. 12. 20.

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

 

댓글


\