공식 홈페이지!
https://apis.map.kakao.com/ios_v2/docs/map/04_label/02_poi/
공홈도 너무 잘 정리돼있지만
요사람것도 참고했다 굿굿
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
}
}
}
}
'PROGRAMMING CODE > SWIFT' 카테고리의 다른 글
[SwiftUI] CoreLocation(사용자 위치정보)사용하기 + tuist (삽질 기록^.^) (0) | 2024.01.31 |
---|---|
[Tuist] Tuist 버전맞추기 (mise) (0) | 2024.01.25 |
[Swift] UIkit, SwiftUI에서 서로 뷰 사용하기 (0) | 2023.12.29 |
[Swift] tuist 외부 패키지, 의존성 주입(Tuist SPM) (0) | 2023.12.26 |
[Swift] tuist 외부 패키지, 의존성 주입(Carthage) (1) | 2023.12.26 |