[FAQ] 지도/로컬 API 문의 전 꼭 읽어 주세요.
https://devtalk.kakao.com/t/faq-api/125610
ide → Xcode
언어-> 스위프트
스위프트UI로 개발중이며 화면에 poi를 표시하는데 어려움을 겪고 있습니다
레이어에 포이를 추가한 후 이를 show 해도 poi가 지도에 보이지 않습니다.
아래 코드를 첨부합니다
//
// ContentView.swift
// KakaoMapOpenApi-Sample
//
// Created by chase on 2020/05/19.
// Copyright © 2020 kakao. All rights reserved.
//
import SwiftUI
import KakaoMapsSDK
struct KakaoMapView: UIViewRepresentable {
@Binding var draw: Bool
@ObservedObject var locationManager : LocationManager
func makeUIView(context: Self.Context) -> KMViewContainer {
//need to correct view size
let view: KMViewContainer = KMViewContainer(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
context.coordinator.createController(view)
context.coordinator.controller?.initEngine()
return view
}
/// Updates the presented `UIView` (and coordinator) to the latest
/// configuration.
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(locationManager: locationManager)
}
/// Cleans up the presented `UIView` (and coordinator) in
/// anticipation of their removal.
static func dismantleUIView(_ uiView: KMViewContainer, coordinator: KakaoMapCoordinator) {
}
class KakaoMapCoordinator: NSObject, MapControllerDelegate {
var locationManager : LocationManager
var controller: KMController?
var first: Bool
var auth: Bool
// override init() {
// first = true
// auth = false
// super.init()
// }
func createLabelLayer(){
print(“creatLabelLayer”)
let view = controller?.getView(“mapview”) as! KakaoMap
let manager = view.getLabelManager()
let layerOption = LabelLayerOptions(layerID: “PoiLayer”, competitionType: .none, competitionUnit: .symbolFirst, orderType: .rank, zOrder: 10001)
let _ = manager.addLabelLayer(option: layerOption)
}
func createPoiStyle(){
print(“creatpoiStyle”)
let view = controller?.getView(“mapview”) as! KakaoMap
let manager = view.getLabelManager()
//let noti1 = PoiBadge(badgeID: "badge1", image: UIImage(named: "MarkerSelected.png"), offset: CGPoint(x: 0.9, y: 0.1), zOrder: 0)
// let iconStyle1 = PoiIconStyle(symbol: UIImage(named: "MarkerSelected.png"), anchorPoint: CGPoint(x: 0.0, y: 0.5), badges: [noti1])
// let noti2 = PoiBadge(badgeID: "badge2", image: UIImage(named: "MarkerUnselected.png"), offset: CGPoint(x: 0.9, y: 0.1), zOrder: 0)
// let iconStyle2 = PoiIconStyle(symbol: UIImage(named: "MarkerUnselected.png"), anchorPoint: CGPoint(x: 0.0, y: 0.5), badges: [noti2])
// let iconStyle1 = PoiIconStyle(symbol: UIImage(named: “MarkerSelected.png”), anchorPoint: CGPoint(x: 0.0, y: 0.5))
// let iconStyle2 = PoiIconStyle(symbol: UIImage(named: “MarkerUnselected.png”), anchorPoint: CGPoint(x: 0.0, y: 0.5))
let icon1 = PoiIconStyle(symbol: UIImage(named: “MarkerUnselected.png”), anchorPoint: CGPoint(x: 0.0, y: 0.5))
let perLevelStyle1 = PerLevelPoiStyle(iconStyle: icon1, level: 0)
let poiStyle1 = PoiStyle(styleID: “nonAdded”, styles: [perLevelStyle1])
manager.addPoiStyle(poiStyle1)
let icon2 = PoiIconStyle(symbol: UIImage(named: "MarkerUnselected.png"), anchorPoint: CGPoint(x: 0.0, y: 0.5))
let perLevelStyle2 = PerLevelPoiStyle(iconStyle: icon2, level: 0)
let poiStyle2 = PoiStyle(styleID: "Added", styles: [perLevelStyle2])
manager.addPoiStyle(poiStyle2)
// let poiStyle = PoiStyle(styleID: “perLavelStyle”, styles: [
// PerLevelPoiStyle(iconStyle: iconStyle1, level: 5),
// PerLevelPoiStyle(iconStyle: iconStyle2, level: 11)]
// )
// manager.addPoiStyle(poiStyle)
}
func createPois(){
print("createPois")
let view = controller?.getView("mapview") as! KakaoMap
let manager = view .getLabelManager()
let layer = manager.getLabelLayer(layerID: "PoiLayer")
layer?.visible = true
let poiOption1 = PoiOptions(styleID: "PerLevelStyle1")
poiOption1.rank = 0
poiOption1.clickable = true
let poiOption2 = PoiOptions(styleID: "PerLevelStyle2")
poiOption2.rank = 0
poiOption2.clickable = true
let poi1 = layer?.addPoi(option:poiOption1, at: MapPoint(longitude: locationManager.lon ?? 126.978365,
latitude: locationManager.lat ?? 37.566691))
// Poi 개별 Badge추가. 즉, 아래에서 생성된 Poi는 Style에 빌트인되어있는 badge와, Poi가 개별적으로 가지고 있는 Badge를 갖게 된다.
let poi2 = layer?.addPoi(option:poiOption2, at: MapPoint(longitude: 126.039000, latitude: 37.109020))
print(mapPoints)
let pois = layer?.addPois(option: poiOption1, at: mapPoints)
layer?.showAllPois()
// let badge = PoiBadge(badgeID: “noti1”, image: UIImage(named: “MarkerUnselected.png”), offset: CGPoint(x: 0, y: 0), zOrder: 1)
// poi1?.addBadge(badge)
poi1?.show()
print(“poi1 is showed? → (poi1!.isShow as Any)”)
print(poi1?.itemID as Any)
poi1?.showBadge(badgeID: “noti1”)
// let badge2 = PoiBadge(badgeID: “noti2”, image: UIImage(named: “MarkerUnselected.png”), offset: CGPoint(x: 0, y: 0), zOrder: 2)
// poi2?.addBadge(badge2)
poi2?.show()
print(“poi2 is showed? → (poi2!.isShow as Any)”)
print(poi2?.itemID as Any)
poi2?.showBadge(badgeID: "noti2")
if let pois = pois {
for poi in pois {
print(poi.itemID)
}
}
}
init(locationManager: LocationManager) {
self.locationManager = locationManager
first = true
auth = false
super.init()
}
func createController(_ view: KMViewContainer) {
controller = KMController(viewContainer: view)
controller?.delegate = self
}
func addViews() {
// var simplePoi : SimplePOI = SimplePOI(co)
// print(“여기다 여기 여기를 봐라 \n\n\n\n (locationManager.lon) // (locationManager.lat)\n\n\n”)
let defaultPosition: MapPoint = MapPoint(longitude: locationManager.lon ?? 127.1, latitude: locationManager.lat ?? 37.2)
let mapviewInfo: MapviewInfo = MapviewInfo(viewName: "mapview", viewInfoName: "map", defaultPosition: defaultPosition)
if controller?.addView(mapviewInfo) == Result.OK {
let _ = controller?.getView("mapview") as! KakaoMap
}
//
createLabelLayer()
createPoiStyle()
createPois()
}
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 {
// print("여기다 여기 여기를 봐라 여기 카메라 업데이트가 있다\n\n\n\n \(locationManager.lon) // \(locationManager.lat)\n\n\n")
let cameraUpdate: CameraUpdate = CameraUpdate.make(target: MapPoint(longitude: locationManager.lon ?? 126.978365,
latitude: locationManager.lat ?? 37.566691),
zoomLevel: 12, mapView: mapView!)
mapView?.moveCamera(cameraUpdate)
first = false
}
}
func authenticationSucceeded() {
auth = true
}
}
}
아래는 해당 코드의 콘솔입니다.
createPois
[<KakaoMapsSDK.MapPoint: 0x6000002a64e0>, <KakaoMapsSDK.MapPoint: 0x6000002a6660>, <KakaoMapsSDK.MapPoint: 0x6000002a6680>, <KakaoMapsSDK.MapPoint: 0x6000002a66a0>]
poi1 is showed? → false
Optional(“agpPoiLayer0”)
poi2 is showed? → false
Optional(“agpPoiLayer1”)
agpPoiLayer2
agpPoiLayer3
agpPoiLayer4
agpPoiLayer5