4 * AsyncRouteManager usage example for Swift
5 * Demonstrates working with async route planning, session management, and navigation
7class AsyncRouteManagerExample: NSObject {
8 private var sdk: NCNavigineSdk?
9 private var locationManager: NCLocationManager?
10 private var navigationManager: NCNavigationManager?
11 private var asyncRouteManager: NCAsyncRouteManager?
12 private var currentSession: NCRouteSession?
17 setupAsyncRouteListener()
21 * Initialize SDK and get managers
23 private func initializeSdk() {
25 // [swift_NavigineSdk_getInstance]
27 sdk = NCNavigineSdk.getInstance()
28 // [swift_NavigineSdk_getInstance]
30 // [swift_NavigineSdk_setUserHash]
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [swift_NavigineSdk_setUserHash]
35 // [swift_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [swift_NavigineSdk_setServer]
40 // [swift_NavigineSdk_getLocationManager]
41 // Get LocationManager for working with locations
42 locationManager = sdk?.getLocationManager()
43 // [swift_NavigineSdk_getLocationManager]
45 // [swift_NavigineSdk_getNavigationManager]
46 // Get NavigationManager for working with navigation
47 navigationManager = sdk?.getNavigationManager(locationManager)
48 // [swift_NavigineSdk_getNavigationManager]
50 // [swift_NavigineSdk_getAsyncRouteManager]
51 // Get AsyncRouteManager for working with async routes
52 asyncRouteManager = sdk?.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
53 // [swift_NavigineSdk_getAsyncRouteManager]
55 if let locationManager = locationManager,
56 let navigationManager = navigationManager,
57 let asyncRouteManager = asyncRouteManager {
58 print("LocationManager, NavigationManager and AsyncRouteManager successfully initialized")
61 print("Error initializing SDK: \(error)")
66 * Setup async route listener
68 private func setupAsyncRouteListener() {
69 // AsyncRouteListener is implemented via protocol extension
70 // No additional setup needed for Swift
74 * Demonstrate AsyncRouteManager methods
76 func demonstrateAsyncRouteManagerMethods() {
77 guard let manager = asyncRouteManager else {
78 print("AsyncRouteManager not initialized")
82 // Create test destination point
83 let destinationPoint = NCPoint(x: 100.0, y: 100.0)
84 let destinationLocationPoint = NCLocationPoint(point: destinationPoint, locationId: 12345, sublocationId: 1)
86 // Create route options
87 let routeOptions = NCRouteOptions(smoothRadius: 2.0, maxProjectionDistance: 5.0, maxAdvance: 2.0)
89 // [swift_AsyncRouteManager_createRouteSession]
90 // Create route session with default graph tag
91 let session = manager.createRouteSession(destinationLocationPoint, routeOptions: routeOptions)
92 print("Created route session with default graph tag")
93 // [swift_AsyncRouteManager_createRouteSession]
95 // [swift_AsyncRouteManager_createRouteSessionWithTag]
96 // Create route session with specific graph tag
97 let sessionWithTag = manager.createRouteSession(withTag: destinationLocationPoint, routeOptions: routeOptions, tag: "main")
98 print("Created route session with 'main' graph tag")
99 // [swift_AsyncRouteManager_createRouteSessionWithTag]
101 // Store session for later use
102 currentSession = session
104 // Add listener to session
105 // [swift_RouteSession_addRouteListener]
106 // Add route listener to session
107 session?.addRouteListener(self)
108 // [swift_RouteSession_addRouteListener]
112 * Demonstrate RoutePath usage
114 func demonstrateRoutePathUsage(_ routePath: NCRoutePath) {
115 guard let routePath = routePath else {
116 print("RoutePath is null")
120 // [swift_RoutePath_getLength]
122 let length = routePath.getLength()
123 print("Route length: \(length) meters")
124 // [swift_RoutePath_getLength]
126 // [swift_RoutePath_getPoints]
128 let points = routePath.getPoints()
129 print("Number of route points: \(points.count)")
130 for (index, point) in points.prefix(3).enumerated() {
131 print("Point \(index + 1): \(point)")
133 // [swift_RoutePath_getPoints]
135 // [swift_RoutePath_getEvents]
137 let events = routePath.getEvents()
138 print("Number of route events: \(events.count)")
139 for event in events.prefix(3) {
140 demonstrateRouteEventUsage(event)
142 // [swift_RoutePath_getEvents]
144 // [swift_RoutePath_head]
145 // Get route head (first 10 meters)
146 if let head = routePath.head(10.0) {
147 print("Route head length: \(head.getLength()) meters")
149 // [swift_RoutePath_head]
151 // [swift_RoutePath_tail]
152 // Get route tail (remaining after 10 meters)
153 if let tail = routePath.tail(10.0) {
154 print("Route tail length: \(tail.getLength()) meters")
156 // [swift_RoutePath_tail]
160 * Demonstrate RouteEvent usage
162 func demonstrateRouteEventUsage(_ event: NCRouteEvent) {
163 guard let event = event else {
164 print("RouteEvent is null")
168 // [swift_RouteEvent_getType]
170 let type = event.getType()
171 print("Event type: \(type)")
172 // [swift_RouteEvent_getType]
174 // [swift_RouteEvent_getValue]
176 let value = event.getValue()
177 print("Event value: \(value)")
178 // [swift_RouteEvent_getValue]
180 // [swift_RouteEvent_getDistance]
181 // Get event distance
182 let distance = event.getDistance()
183 print("Event distance: \(distance) meters")
184 // [swift_RouteEvent_getDistance]
186 // [swift_RouteEvent_constructor]
187 // Create new route event
188 let newEvent = NCRouteEvent(type: .turnLeft, value: 90, distance: 50.0)
189 print("Created new route event: \(newEvent.getType()) at \(newEvent.getDistance()) meters")
190 // [swift_RouteEvent_constructor]
194 * Demonstrate RouteEventType enum values
196 func demonstrateRouteEventTypes() {
197 // [swift_RouteEventType_values]
198 // Get all route event type values
199 let types: [NCRouteEventType] = [.turnLeft, .turnRight, .transition]
200 print("Available route event types:")
204 // [swift_RouteEventType_values]
208 * Demonstrate LocationPoint usage
210 func demonstrateLocationPointUsage(_ locationPoint: NCLocationPoint) {
211 guard let locationPoint = locationPoint else {
212 print("LocationPoint is null")
216 // [swift_LocationPoint_getPoint]
217 // Get point coordinates
218 let point = locationPoint.getPoint()
219 print("Location point: (\(point.getX()), \(point.getY()))")
220 // [swift_LocationPoint_getPoint]
222 // [swift_LocationPoint_getLocationId]
224 let locationId = locationPoint.getLocationId()
225 print("Location ID: \(locationId)")
226 // [swift_LocationPoint_getLocationId]
228 // [swift_LocationPoint_getSublocationId]
229 // Get sublocation ID
230 let sublocationId = locationPoint.getSublocationId()
231 print("Sublocation ID: \(sublocationId)")
232 // [swift_LocationPoint_getSublocationId]
234 // [swift_LocationPoint_constructor]
235 // Create new location point
236 let newPoint = NCPoint(x: 25.0, y: 35.0)
237 let newLocationPoint = NCLocationPoint(point: newPoint, locationId: 12345, sublocationId: 2)
238 print("Created new location point: \(newLocationPoint.getPoint().getX()), \(newLocationPoint.getPoint().getY())")
239 // [swift_LocationPoint_constructor]
243 * Demonstrate Point usage
245 func demonstratePointUsage(_ point: NCPoint) {
246 guard let point = point else {
247 print("Point is null")
251 // [swift_Point_getX]
254 print("Point X: \(x)")
255 // [swift_Point_getX]
257 // [swift_Point_getY]
260 print("Point Y: \(y)")
261 // [swift_Point_getY]
263 // [swift_Point_constructor]
265 let newPoint = NCPoint(x: 15.0, y: 25.0)
266 print("Created new point: (\(newPoint.getX()), \(newPoint.getY()))")
267 // [swift_Point_constructor]
271 * Demonstrate RouteOptions usage
273 func demonstrateRouteOptions() {
274 print("=== Route Options ===")
276 // [swift_RouteOptions_constructor]
277 // Create route options with custom parameters
278 let routeOptions = NCRouteOptions(smoothRadius: 3.0, maxProjectionDistance: 7.0, maxAdvance: 2.5)
279 print("Created route options with smoothRadius: \(routeOptions.getSmoothRadius()), maxProjectionDistance: \(routeOptions.getMaxProjectionDistance()), maxAdvance: \(routeOptions.getMaxAdvance())")
280 // [swift_RouteOptions_constructor]
282 // Demonstrate different route options configurations
284 NCRouteOptions(smoothRadius: 0.0, maxProjectionDistance: 5.0, maxAdvance: 2.0), // No smoothing
285 NCRouteOptions(smoothRadius: 2.0, maxProjectionDistance: 10.0, maxAdvance: 1.0), // High smoothing, high projection distance
286 NCRouteOptions(smoothRadius: 1.0, maxProjectionDistance: 3.0, maxAdvance: 3.0), // Low smoothing, low projection distance
289 for (index, options) in optionsList.enumerated() {
290 print("Options \(index + 1): smoothRadius=\(options.getSmoothRadius()), maxProjectionDistance=\(options.getMaxProjectionDistance()), maxAdvance=\(options.getMaxAdvance())")
295 * Demonstrate advanced async routing features
297 func demonstrateAdvancedAsyncRoutingFeatures() {
298 print("=== Advanced Async Routing Features ===")
300 guard let manager = asyncRouteManager else {
304 // Create multiple destination points
306 NCLocationPoint(point: NCPoint(x: 50.0, y: 50.0), locationId: 12345, sublocationId: 1),
307 NCLocationPoint(point: NCPoint(x: 100.0, y: 100.0), locationId: 12345, sublocationId: 1),
308 NCLocationPoint(point: NCPoint(x: 150.0, y: 150.0), locationId: 12345, sublocationId: 1)
311 // Create different route options for each destination
313 NCRouteOptions(smoothRadius: 2.0, maxProjectionDistance: 5.0, maxAdvance: 2.0),
314 NCRouteOptions(smoothRadius: 3.0, maxProjectionDistance: 7.0, maxAdvance: 1.5),
315 NCRouteOptions(smoothRadius: 1.0, maxProjectionDistance: 3.0, maxAdvance: 2.5),
318 // Create sessions for each destination
319 var sessions: [NCRouteSession] = []
320 for (index, destination) in destinations.enumerated() {
321 let session = manager.createRouteSession(destination, routeOptions: optionsList[index])
322 sessions.append(session!)
323 print("Created session \(index + 1) for destination: \(destination.getPoint().getX()), \(destination.getPoint().getY())")
325 session?.addRouteListener(self)
327 Thread.sleep(forTimeInterval: 1.0)
330 // Wait for route updates
331 Thread.sleep(forTimeInterval: 3.0)
333 // Cancel all sessions
334 for (index, session) in sessions.enumerated() {
335 // [swift_AsyncRouteManager_cancelRouteSession]
336 // Cancel route session
337 manager.cancelRouteSession(session)
338 print("Cancelled session \(index + 1)")
339 // [swift_AsyncRouteManager_cancelRouteSession]
341 Thread.sleep(forTimeInterval: 1.0)
346 * Demonstrate RouteStatus enum usage
348 func demonstrateRouteStatusUsage() {
349 print("=== RouteStatus Enum Usage ===")
351 // [swift_RouteStatus_values]
352 // Demonstrate all RouteStatus values
353 print("Available RouteStatus values:")
354 let allStatuses: [NCRouteStatus] = [.missingGraph, .missingPosition, .missingRoute, .missingProjection, .newRoute]
355 for status in allStatuses {
356 print(" - \(status): \(status.rawValue)")
359 // Demonstrate status checking
360 let testStatus: NCRouteStatus = .newRoute
363 print("Router is ready for navigation")
365 print("Router is missing the route graph")
366 case .missingPosition:
367 print("Router is missing the current position")
368 case .missingProjection:
369 print("Current position is off the route graph")
371 print("Router unable to find the route to the destination point")
373 // [swift_RouteStatus_values]
377 * Demonstrate route session management
379 func demonstrateRouteSessionManagement() {
380 print("=== Route Session Management ===")
382 guard let manager = asyncRouteManager else {
386 // Create test destination
387 let destinationPoint = NCPoint(x: 75.0, y: 75.0)
388 let destinationLocationPoint = NCLocationPoint(point: destinationPoint, locationId: 12345, sublocationId: 1)
389 let routeOptions = NCRouteOptions(smoothRadius: 2.0, maxProjectionDistance: 5.0, maxAdvance: 2.0)
391 // Create session with default tag
392 let defaultSession = manager.createRouteSession(destinationLocationPoint, routeOptions: routeOptions)
393 print("Created session with default tag")
395 // Create session with specific tag
396 let taggedSession = manager.createRouteSession(withTag: destinationLocationPoint, routeOptions: routeOptions, tag: "elevator")
397 print("Created session with 'elevator' tag")
399 // Add listeners to both sessions
400 defaultSession?.addRouteListener(self)
401 taggedSession?.addRouteListener(self)
403 // Simulate some time passing
404 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
406 // [swift_RouteSession_removeRouteListener]
407 // Remove route listener from session
408 defaultSession?.removeRouteListener(self)
409 taggedSession?.removeRouteListener(self)
410 print("Removed listeners from sessions")
411 // [swift_RouteSession_removeRouteListener]
414 manager.cancelRouteSession(defaultSession)
415 manager.cancelRouteSession(taggedSession)
416 print("Cancelled both sessions")
424 if let session = currentSession {
425 // [swift_RouteSession_removeRouteListener]
426 // Remove route listener
427 session.removeRouteListener(self)
428 // [swift_RouteSession_removeRouteListener]
431 if let manager = asyncRouteManager, let session = currentSession {
432 // [swift_AsyncRouteManager_cancelRouteSession_1]
433 // Cancel route session
434 manager.cancelRouteSession(session)
435 // [swift_AsyncRouteManager_cancelRouteSession_1]
440 * Main demonstration method
443 print("=== AsyncRouteManager Example ===")
445 demonstrateAsyncRouteManagerMethods()
446 demonstrateRouteEventTypes()
447 demonstrateRouteOptions()
448 demonstrateRouteStatusUsage()
449 demonstrateRouteSessionManagement()
450 demonstrateAdvancedAsyncRoutingFeatures()
452 // Wait a bit for route updates
453 Thread.sleep(forTimeInterval: 3.0)
456 print("=== Example completed ===")
460// MARK: - NCAsyncRouteListener
462extension AsyncRouteManagerExample: NCAsyncRouteListener {
463 // [swift_AsyncRouteListener_onRouteChanged]
464 func onRouteChanged(_ status: NCRouteStatus, currentPath: NCRoutePath) {
465 print("Route changed with status: \(status)")
466 if status == .newRoute && currentPath != nil {
467 demonstrateRoutePathUsage(currentPath)
469 print("Route not ready, status: \(status)")
472 // [swift_AsyncRouteListener_onRouteChanged]
474 // [swift_AsyncRouteListener_onRouteAdvanced]
475 func onRouteAdvanced(_ distance: Float, point: NCLocationPoint) {
476 print("Route advanced: \(distance) meters")
477 demonstrateLocationPointUsage(point)
479 // [swift_AsyncRouteListener_onRouteAdvanced]
483 * Function to run the example
486 let example = AsyncRouteManagerExample()