Loading...
Searching...
No Matches
AsyncRouteManagerExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * AsyncRouteManager usage example for Swift
5 * Demonstrates working with async route planning, session management, and navigation
6 */
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?
13
14 override init() {
15 super.init()
16 initializeSdk()
17 setupAsyncRouteListener()
18 }
19
20 /**
21 * Initialize SDK and get managers
22 */
23 private func initializeSdk() {
24 do {
25 // [swift_NavigineSdk_getInstance]
26 // Get SDK instance
27 sdk = NCNavigineSdk.getInstance()
28 // [swift_NavigineSdk_getInstance]
29
30 // [swift_NavigineSdk_setUserHash]
31 // Set user hash
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [swift_NavigineSdk_setUserHash]
34
35 // [swift_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [swift_NavigineSdk_setServer]
39
40 // [swift_NavigineSdk_getLocationManager]
41 // Get LocationManager for working with locations
42 locationManager = sdk?.getLocationManager()
43 // [swift_NavigineSdk_getLocationManager]
44
45 // [swift_NavigineSdk_getNavigationManager]
46 // Get NavigationManager for working with navigation
47 navigationManager = sdk?.getNavigationManager(locationManager)
48 // [swift_NavigineSdk_getNavigationManager]
49
50 // [swift_NavigineSdk_getAsyncRouteManager]
51 // Get AsyncRouteManager for working with async routes
52 asyncRouteManager = sdk?.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
53 // [swift_NavigineSdk_getAsyncRouteManager]
54
55 if let locationManager = locationManager,
56 let navigationManager = navigationManager,
57 let asyncRouteManager = asyncRouteManager {
58 print("LocationManager, NavigationManager and AsyncRouteManager successfully initialized")
59 }
60 } catch {
61 print("Error initializing SDK: \‍(error)")
62 }
63 }
64
65 /**
66 * Setup async route listener
67 */
68 private func setupAsyncRouteListener() {
69 // AsyncRouteListener is implemented via protocol extension
70 // No additional setup needed for Swift
71 }
72
73 /**
74 * Demonstrate AsyncRouteManager methods
75 */
76 func demonstrateAsyncRouteManagerMethods() {
77 guard let manager = asyncRouteManager else {
78 print("AsyncRouteManager not initialized")
79 return
80 }
81
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)
85
86 // Create route options
87 let routeOptions = NCRouteOptions(smoothRadius: 2.0, maxProjectionDistance: 5.0, maxAdvance: 2.0)
88
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]
94
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]
100
101 // Store session for later use
102 currentSession = session
103
104 // Add listener to session
105 // [swift_RouteSession_addRouteListener]
106 // Add route listener to session
107 session?.addRouteListener(self)
108 // [swift_RouteSession_addRouteListener]
109 }
110
111 /**
112 * Demonstrate RoutePath usage
113 */
114 func demonstrateRoutePathUsage(_ routePath: NCRoutePath) {
115 guard let routePath = routePath else {
116 print("RoutePath is null")
117 return
118 }
119
120 // [swift_RoutePath_getLength]
121 // Get route length
122 let length = routePath.getLength()
123 print("Route length: \‍(length) meters")
124 // [swift_RoutePath_getLength]
125
126 // [swift_RoutePath_getPoints]
127 // Get route points
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)")
132 }
133 // [swift_RoutePath_getPoints]
134
135 // [swift_RoutePath_getEvents]
136 // Get route events
137 let events = routePath.getEvents()
138 print("Number of route events: \‍(events.count)")
139 for event in events.prefix(3) {
140 demonstrateRouteEventUsage(event)
141 }
142 // [swift_RoutePath_getEvents]
143
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")
148 }
149 // [swift_RoutePath_head]
150
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")
155 }
156 // [swift_RoutePath_tail]
157 }
158
159 /**
160 * Demonstrate RouteEvent usage
161 */
162 func demonstrateRouteEventUsage(_ event: NCRouteEvent) {
163 guard let event = event else {
164 print("RouteEvent is null")
165 return
166 }
167
168 // [swift_RouteEvent_getType]
169 // Get event type
170 let type = event.getType()
171 print("Event type: \‍(type)")
172 // [swift_RouteEvent_getType]
173
174 // [swift_RouteEvent_getValue]
175 // Get event value
176 let value = event.getValue()
177 print("Event value: \‍(value)")
178 // [swift_RouteEvent_getValue]
179
180 // [swift_RouteEvent_getDistance]
181 // Get event distance
182 let distance = event.getDistance()
183 print("Event distance: \‍(distance) meters")
184 // [swift_RouteEvent_getDistance]
185
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]
191 }
192
193 /**
194 * Demonstrate RouteEventType enum values
195 */
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:")
201 for type in types {
202 print(" - \‍(type)")
203 }
204 // [swift_RouteEventType_values]
205 }
206
207 /**
208 * Demonstrate LocationPoint usage
209 */
210 func demonstrateLocationPointUsage(_ locationPoint: NCLocationPoint) {
211 guard let locationPoint = locationPoint else {
212 print("LocationPoint is null")
213 return
214 }
215
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]
221
222 // [swift_LocationPoint_getLocationId]
223 // Get location ID
224 let locationId = locationPoint.getLocationId()
225 print("Location ID: \‍(locationId)")
226 // [swift_LocationPoint_getLocationId]
227
228 // [swift_LocationPoint_getSublocationId]
229 // Get sublocation ID
230 let sublocationId = locationPoint.getSublocationId()
231 print("Sublocation ID: \‍(sublocationId)")
232 // [swift_LocationPoint_getSublocationId]
233
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]
240 }
241
242 /**
243 * Demonstrate Point usage
244 */
245 func demonstratePointUsage(_ point: NCPoint) {
246 guard let point = point else {
247 print("Point is null")
248 return
249 }
250
251 // [swift_Point_getX]
252 // Get X coordinate
253 let x = point.getX()
254 print("Point X: \‍(x)")
255 // [swift_Point_getX]
256
257 // [swift_Point_getY]
258 // Get Y coordinate
259 let y = point.getY()
260 print("Point Y: \‍(y)")
261 // [swift_Point_getY]
262
263 // [swift_Point_constructor]
264 // Create new point
265 let newPoint = NCPoint(x: 15.0, y: 25.0)
266 print("Created new point: (\‍(newPoint.getX()), \‍(newPoint.getY()))")
267 // [swift_Point_constructor]
268 }
269
270 /**
271 * Demonstrate RouteOptions usage
272 */
273 func demonstrateRouteOptions() {
274 print("=== Route Options ===")
275
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]
281
282 // Demonstrate different route options configurations
283 let optionsList = [
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
287 ]
288
289 for (index, options) in optionsList.enumerated() {
290 print("Options \‍(index + 1): smoothRadius=\‍(options.getSmoothRadius()), maxProjectionDistance=\‍(options.getMaxProjectionDistance()), maxAdvance=\‍(options.getMaxAdvance())")
291 }
292 }
293
294 /**
295 * Demonstrate advanced async routing features
296 */
297 func demonstrateAdvancedAsyncRoutingFeatures() {
298 print("=== Advanced Async Routing Features ===")
299
300 guard let manager = asyncRouteManager else {
301 return
302 }
303
304 // Create multiple destination points
305 let destinations = [
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)
309 ]
310
311 // Create different route options for each destination
312 let optionsList = [
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),
316 ]
317
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())")
324
325 session?.addRouteListener(self)
326
327 Thread.sleep(forTimeInterval: 1.0)
328 }
329
330 // Wait for route updates
331 Thread.sleep(forTimeInterval: 3.0)
332
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]
340
341 Thread.sleep(forTimeInterval: 1.0)
342 }
343 }
344
345 /**
346 * Demonstrate RouteStatus enum usage
347 */
348 func demonstrateRouteStatusUsage() {
349 print("=== RouteStatus Enum Usage ===")
350
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)")
357 }
358
359 // Demonstrate status checking
360 let testStatus: NCRouteStatus = .newRoute
361 switch testStatus {
362 case .newRoute:
363 print("Router is ready for navigation")
364 case .missingGraph:
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")
370 case .missingRoute:
371 print("Router unable to find the route to the destination point")
372 }
373 // [swift_RouteStatus_values]
374 }
375
376 /**
377 * Demonstrate route session management
378 */
379 func demonstrateRouteSessionManagement() {
380 print("=== Route Session Management ===")
381
382 guard let manager = asyncRouteManager else {
383 return
384 }
385
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)
390
391 // Create session with default tag
392 let defaultSession = manager.createRouteSession(destinationLocationPoint, routeOptions: routeOptions)
393 print("Created session with default tag")
394
395 // Create session with specific tag
396 let taggedSession = manager.createRouteSession(withTag: destinationLocationPoint, routeOptions: routeOptions, tag: "elevator")
397 print("Created session with 'elevator' tag")
398
399 // Add listeners to both sessions
400 defaultSession?.addRouteListener(self)
401 taggedSession?.addRouteListener(self)
402
403 // Simulate some time passing
404 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
405 // Remove listeners
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]
412
413 // Cancel sessions
414 manager.cancelRouteSession(defaultSession)
415 manager.cancelRouteSession(taggedSession)
416 print("Cancelled both sessions")
417 }
418 }
419
420 /**
421 * Clean up resources
422 */
423 func cleanup() {
424 if let session = currentSession {
425 // [swift_RouteSession_removeRouteListener]
426 // Remove route listener
427 session.removeRouteListener(self)
428 // [swift_RouteSession_removeRouteListener]
429 }
430
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]
436 }
437 }
438
439 /**
440 * Main demonstration method
441 */
442 func runExample() {
443 print("=== AsyncRouteManager Example ===")
444
445 demonstrateAsyncRouteManagerMethods()
446 demonstrateRouteEventTypes()
447 demonstrateRouteOptions()
448 demonstrateRouteStatusUsage()
449 demonstrateRouteSessionManagement()
450 demonstrateAdvancedAsyncRoutingFeatures()
451
452 // Wait a bit for route updates
453 Thread.sleep(forTimeInterval: 3.0)
454
455 cleanup()
456 print("=== Example completed ===")
457 }
458}
459
460// MARK: - NCAsyncRouteListener
461
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)
468 } else {
469 print("Route not ready, status: \‍(status)")
470 }
471 }
472 // [swift_AsyncRouteListener_onRouteChanged]
473
474 // [swift_AsyncRouteListener_onRouteAdvanced]
475 func onRouteAdvanced(_ distance: Float, point: NCLocationPoint) {
476 print("Route advanced: \‍(distance) meters")
477 demonstrateLocationPointUsage(point)
478 }
479 // [swift_AsyncRouteListener_onRouteAdvanced]
480}
481
482/**
483 * Function to run the example
484 */
485func main() {
486 let example = AsyncRouteManagerExample()
487 example.runExample()
488}
489
490// Run the example
491main()