본문 바로가기
PROGRAMMING CODE/SWIFT

[SwiftUI] KakaoMapView (Xcode SPM, SwiftUI 사용)

by daye_ 2024. 1. 16.

 

공식 홈페이지!

https://apis.map.kakao.com/ios_v2/docs/map/04_label/02_poi/

 

공홈도 너무 잘 정리돼있지만

 

 

 

 

[Swift] SwiftUI로 Kakao Map 보여주기

KakaoMapsSDK v.2 for iOS 문서를 보고 정리한 글입니다. 더 자세한 내용 위 링크를 참고해 주세요 😊 (오타정정 및 피드백 대환영🙌🏻) 1. 앱 등록하기 kakao developers 링크에 들어가서 애플리케이션 추

zoeful-log.tistory.com

요사람것도 참고했다 굿굿

 

 

 

SwiftUI로 맵뷰 띄우기!

 

 

import SwiftUI
import KakaoMapsSDK

struct ContentView: View {
    @State var draw: Bool = true
    var body: some View {
        KakaoMapView(draw: $draw).onAppear(perform: {
            self.draw = true
        }).onDisappear(perform: {
            self.draw = false
        }).frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

//SwiftUI에서 UIkit코드 사용
struct KakaoMapView: UIViewRepresentable {
    @Binding var draw: Bool
    
    func makeUIView(context: Self.Context) -> KMViewContainer {
        let view: KMViewContainer = KMViewContainer()
        view.sizeToFit()
        context.coordinator.createController(view)
        context.coordinator.controller?.initEngine()
        
        return view
    }

    func updateUIView(_ uiView: KMViewContainer, context: Self.Context) {
        if draw {
        //메인스레드에서 뷰 그려주기
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                context.coordinator.controller?.startEngine()
                context.coordinator.controller?.startRendering()
            }
        }
        else {
            context.coordinator.controller?.stopRendering()
            context.coordinator.controller?.stopEngine()
        }
    }
    
    func makeCoordinator() -> KakaoMapCoordinator {
        return KakaoMapCoordinator()
    }

    static func dismantleUIView(_ uiView: KMViewContainer, coordinator: KakaoMapCoordinator) {

    }
  
    class KakaoMapCoordinator: NSObject, MapControllerDelegate {
        var controller: KMController?
        var first: Bool
        
        override init() {
            first = true
            super.init()
        }
        
        func createController(_ view: KMViewContainer) {
            controller = KMController(viewContainer: view)
            controller?.delegate = self
        }
        
        func addViews() {
            let defaultPosition: MapPoint = MapPoint(longitude: 127.108678, latitude: 37.402001)
            let mapviewInfo: MapviewInfo = MapviewInfo(viewName: "mapview", viewInfoName: "map", defaultPosition: defaultPosition)
            
            if controller?.addView(mapviewInfo) == Result.OK {
                let _ = controller?.getView("mapview") as! KakaoMap
            }
        }
        
        func containerDidResized(_ size: CGSize) {
            let mapView: KakaoMap? = controller?.getView("mapview") as? KakaoMap
            mapView?.viewRect = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: size)
            if first {
                let cameraUpdate: CameraUpdate = CameraUpdate.make(target: MapPoint(longitude: 127.108678, latitude: 37.402001), zoomLevel: 18, mapView: mapView!)
                mapView?.moveCamera(cameraUpdate)
                first = false
            }
        }
    }
}