//여기서 touch event 처리
func poiTouched(_ poi: Poi) {
print(poi.itemID)
}
func touchesBegan(_ touches: Set<AnyHashable>) {
if let touch = touches.first as? UITouch {
let radius = touch.majorRadius
let touchedCenter = touch.location(in: touch.window)
// touch major radius기준으로 거리 재기 위한 임시 Point
let withRadius = CGPoint(x: touchedCenter.x + radius, y: touchedCenter.y)
if let point = getPosition(touchedCenter),
let withRadiusPoint = getPosition(withRadius)
{
// 거리 계산
let latdist = (point.wgsCoord.latitude - withRadiusPoint.wgsCoord.latitude)
let longdist = (point.wgsCoord.longitude - withRadiusPoint.wgsCoord.longitude)
let powdDist = latdist * latdist + longdist * longdist
let dist = sqrt(powdDist) // radius의 map상에서의 거리
if let touchedPoi = touchedPOI(point.wgsCoord, dist) {
poiTouched(touchedPoi)
}
}
}
}
private func touchedPOI(_ coord: GeoCoordinate, _ dist: Double) -> Poi? {
if let map = controller?.getView("mapview") as? KakaoMap {
let manager = map.getLabelManager()
let layer = manager.getLabelLayer(layerID: "PoiLayer")
guard let pois = layer?.getAllPois() else {return nil}
var touchedPois: [Poi : Double] = [:] // value = distance
for poi in pois {
let latdist = (coord.latitude - poi.position.wgsCoord.latitude)
let longdist = (coord.longitude - poi.position.wgsCoord.longitude)
let powdDist = latdist * latdist + longdist * longdist
let distWithPoi = sqrt(powdDist)
// touched radius 반경 안에 있는 경우
if distWithPoi < dist {
touchedPois[poi] = distWithPoi
}
}
if touchedPois.isEmpty { return nil }
else { return touchedPois.sorted(by: {$0.value < $1.value}).first!.key} // 가장 가까운 poi를 리턴합니다.
}
return nil
}
private func getPosition(_ point: CGPoint) -> MapPoint? {
let mapView: KakaoMap? = controller?.getView("mapview") as? KakaoMap
guard let map = mapView else
{return nil}
return map.getPosition(point)
}
이렇게 임시 구현해봤습니당, 억지구현인거 같긴 한데 임시방편으로 이렇게라도 touch event 만들어봤어요