[FAQ] 지도/로컬 API 문의 전 꼭 읽어 주세요.
https://devtalk.kakao.com/t/faq-api/125610
포항과 판교는 지정한 적이 없는데 저렇게 route 를 그리네용…
코드의 어느 부분을 수정해야 하나요?? ㅜㅜ
그리고 1번과 2번 사이의 구불구불한 루트를 생성하고 싶은데요,
시작점과 끝점의 좌표를 어떻게 수정해야 하나요>?
func createRouteLayer(view: KakaoMap) {
let view = controller?.getView("mapview") as? KakaoMap
let manager = view?.getRouteManager()
let _ = manager?.addRouteLayer(layerID: "RouteLayer", zOrder: 2)
}
// StyleSet을 생성한다.
func createRouteStyleSet(view: KakaoMap) {
let view = controller?.getView("mapview") as? KakaoMap
let manager = view?.getRouteManager()
let styleSet = RouteStyleSet(styleID: "routeStyleSet1")
let blackStyle = RouteStyle(styles: [
PerLevelRouteStyle(width: 18, color: UIColor.black, strokeWidth: 0, strokeColor: UIColor.black, level: 2, patternIndex: -1)
])
styleSet.addStyle(blackStyle)
manager?.addRouteStyleSet(styleSet)
}
func createRouteline(view: KakaoMap) {
let view = controller?.getView("mapview") as! KakaoMap
let manager = view.getRouteManager()
let layer = manager.addRouteLayer(layerID: "RouteLayer", zOrder: 0)
let segmentPoints = routeSegmentPoints()
var segments: [RouteSegment] = [RouteSegment]()
var styleIndex: UInt = 0
for points in segmentPoints {
// 경로 포인트로 RouteSegment 생성. 사용할 스타일 인덱스도 지정한다.
let seg = RouteSegment(points: points, styleIndex: styleIndex)
segments.append(seg)
styleIndex = (styleIndex + 1) % 4
}
let options = RouteOptions(routeID: "routes", styleID: "routeStyleSet1", zOrder: 0)
options.segments = segments
let route = layer?.addRoute(option: options)
route?.show()
//let pnt = segments[0].points[0]
//mapView.moveCamera(CameraUpdate.make(target: pnt, zoomLevel: 15, mapView: mapView))
}
func routeSegmentPoints() -> [[MapPoint]] {
//let mapPoint = MapPoint(longitude: 126.978365, latitude: 37.566691)
let point1 = MapPoint(longitude: 129.1667035724, latitude: 35.2958702558)
let point2 = MapPoint(longitude: 129.1695450043, latitude: 35.2954092493)
var segments = [[MapPoint]]()
segments.append([point1, point2])
print("\(segments)")
var coordinate: Bool = false
if let filePath = Bundle.main.path(forResource: "routeSample_car", ofType: "txt") {
do {
let contents = try String(contentsOfFile: filePath)
let lines = contents.components(separatedBy: .newlines)
var index = 0
while index < lines.count - 1 {
let line = lines[index]
//print("현재 처리중인 라인: \(line)") // 현재 라인 출력
if coordinate && line == "end" {
coordinate = false
//print("세그먼트 종료") // 세그먼트 종료를 알림
index += 1
continue
}
if coordinate && line.count > 3 {
let coords = line.components(separatedBy: "|")
var points = [MapPoint]()
for coord in coords {
let pnt = coord.components(separatedBy: ",")
let converted = MapCoordConverter.fromWCongToWGS84(wcong: CartesianCoordinate(x: Double(pnt[0])!, y: Double(pnt[1])!))
points.append(MapPoint(longitude: converted.longitude, latitude: converted.latitude))
}
segments.append(points)
//print("추가된 포인트: \(points)") // 추가된 포인트들 출력
//print("segmets 구경해라 \(segments)")
//for point in points {
//print("추가된 포인트 Longitude: \(point.longitude), Latitude: \(point.latitude)")
//}
}
if line == "route" {
coordinate = true
//print("새로운 세그먼트 시작") // 새로운 세그먼트 시작을 알림
}
index += 1
}
} catch {
//print("파일 읽기 오류: \(error)")
}
}
return segments
}