Skip to content
tyler edited this page Oct 10, 2023 · 1 revision

개발 κ³Όμ •

μš”μ•½

  • μ΅œλŒ€ν•œ SwiftUI λ₯Ό ν™œμš©ν•˜κ³ μž ν–ˆμœΌλ‚˜ 쒋은 κΈ°λŠ₯(?) 듀은 iOS 17.0 버전 이상뢀터 κ°€λŠ₯ν•œ 것듀이 λ§Žμ•˜κ³ , iOS 17.0 버전 μ•„λž˜μ˜ SwiftUI μ—μ„œμ˜ MapKit κ΅¬ν˜„μ€ μ œμ•½μ΄ λ§Žμ•˜μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬ν•˜μ—¬ UIKit 을 μ‚¬μš©ν•˜λ˜ μ΅œλŒ€ν•œ SwiftUI 와 ν˜Έν™˜λ˜λ„λ‘ UIViewRepresentable λ₯Ό μ‚¬μš©ν•˜μ—¬ Map 을 κ΅¬ν˜„ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

iOS 17.0 이상

μ„ νƒν•˜μ§€ μ•Šμ€ 이유

  • iOS 17.0 λ²„μ „μœΌλ‘œ 높은 λ²„μ „μ—μ„œλ§Œ ν˜Έν™˜μ΄ κ°€λŠ₯ν•˜μ—¬ μΆ”ν›„ μ•ˆμ •ν™” λ˜μ—ˆμ„ λ•Œ μ μš©ν•˜λŠ” 것이 쒋을 λ“― ν•©λ‹ˆλ‹€.

μ£Όμš”κΈ°λŠ₯

  • position: MapCameraPosition 둜 지도 ν™”λ©΄ μ‘°μ • κ°€λŠ₯
  • Map(position: $position) { 처럼 μ€‘κ΄„ν˜Έλ₯Ό μ—΄μ–΄ Map 에 넣을 μš”μ†Œλ“€μ„ SwiftUI 만으둜 관리 κ°€λŠ₯'
struct MapView_iOS17: View {
    
    @State private var position: MapCameraPosition = .camera(.init(centerCoordinate: .duckRun, distance: 1800.0, heading: -80.0))
    
    var body: some View {
        Map(position: $position) {
            UserAnnotation()
            Annotation("였리런", coordinate: .duckRun) {
                ZStack {
                    RoundedRectangle(cornerRadius: 10, style: .continuous)
                        .foregroundStyle(.yellow)
                    Image("duck")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 30)
                        .padding(5)
                }
            }
        }
...

iOS 17.0 μ΄ν•˜

μ„ νƒν•˜μ§€ μ•Šμ€ 이유

  • iOS 17.0 μ΄μ „λ²„μ „μ—μ„œ μ§€μ›λ˜λŠ” MapKit 은 μ œμ•½μ΄ λ„ˆλ¬΄ λ§ŽμŠ΅λ‹ˆλ‹€.
    • Map Rotation 을 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ‚¬μš©μžκ°€ 자유둭게 맡을 μ‘°μž‘ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
    • userTrackingMode 와 MKPolyline μͺ½μ—μ„œ μ œν•œμ΄ λ§ŽμŠ΅λ‹ˆλ‹€.
  • λ³΅μž‘ν•œ λ³€μˆ˜ 관리
    • UIKit μˆ˜μ€€μœΌλ‘œ 관리해야 ν•  λ³€μˆ˜κ°€ λ„ˆλ¬΄ 많고 이름이 κΉλ‹ˆλ‹€.
  • λ¬Όλ‘  μ‘°κΈˆλ” digging 해보면 방법이 μžˆκ² μ§€λ§Œ κ²°κ΅­ UIKit 을 ν˜Όμš©ν•΄μ„œ μ‚¬μš©ν•΄μ•Ό ν•  κ²ƒμ΄λΌλŠ” 결둠에 λ‹€λ‹€λžμŠ΅λ‹ˆλ‹€.

μ£Όμš”κΈ°λŠ₯

    // 17.0 μ—μ„œλŠ” MapCameraPosition 에 ν•΄λ‹Ήν•˜λŠ” λΆ€λΆ„ 
    @State private var camera = MKMapCamera(lookingAtCenter: .duckRun, fromDistance: 1800, pitch: 0, heading: -80)

    // Annotation 을 μœ„ν•œ λ³€μˆ˜
    let duckRunPoints = [
        RunPoint(title: "였리런", coordinate: .duckRun, image: "duck"),
        RunPoint(title: "μŠ€νƒ€νŠΈ 포인트", coordinate: CLLocationCoordinate2D(latitude: 37.53958, longitude: 127.07435), image: "startpoint")
    ]

    var body: some View {
        VStack(spacing: 30) {
            Map(coordinateRegion: $region,
                interactionModes: interactionMode,
                showsUserLocation: true,
                userTrackingMode: $userTracking,
                annotationItems: duckRunPoints,
                annotationContent: { location in
                MapAnnotation(coordinate: location.coordinate) {
                    if location.title == "였리런" {
                        VStack {
                            Image(location.image)
                                .resizable()
                                .scaledToFit()
                                .frame(width: 30)
                            Text(location.title)
                                .font(.caption2)
                                .foregroundStyle(.secondary)
                        }
                    } else {
                        VStack {
                            Image(location.image)
                                .resizable()
                                .scaledToFit()
                                .frame(width: 15)
                            Text(location.title)
                                .font(.system(size: 6))
                                .foregroundStyle(.secondary)
                        }
                    }
                }
            })
...

Clone this wiki locally