Loading...
Searching...
No Matches
RouteManagerExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * RouteManager usage example for Swift
5 * Demonstrates working with route planning, path calculation, and navigation
6 */
7class RouteManagerExample: NSObject {
8 private var sdk: NCNavigineSdk?
9 private var locationManager: NCLocationManager?
10 private var navigationManager: NCNavigationManager?
11 private var routeManager: NCRouteManager?
12
13 override init() {
14 super.init()
15 initializeSdk()
16 setupRouteListener()
17 }
18
19 /**
20 * Initialize SDK and get managers
21 */
22 private func initializeSdk() {
23 do {
24 // [swift_NavigineSdk_getInstance]
25 // Get SDK instance
26 sdk = NCNavigineSdk.getInstance()
27 // [swift_NavigineSdk_getInstance]
28
29 // [swift_NavigineSdk_setUserHash]
30 // Set user hash
31 sdk?.setUserHash("USER-HASH-HERE")
32 // [swift_NavigineSdk_setUserHash]
33
34 // [swift_NavigineSdk_setServer]
35 // Set server URL (optional)
36 sdk?.setServer("https://custom.navigine.com")
37 // [swift_NavigineSdk_setServer]
38
39 // [swift_NavigineSdk_getLocationManager]
40 // Get LocationManager for working with locations
41 locationManager = sdk?.getLocationManager()
42 // [swift_NavigineSdk_getLocationManager]
43
44 // [swift_NavigineSdk_getNavigationManager]
45 // Get NavigationManager for working with navigation
46 navigationManager = sdk?.getNavigationManager(locationManager)
47 // [swift_NavigineSdk_getNavigationManager]
48
49 // [swift_NavigineSdk_getRouteManager]
50 // Get RouteManager for working with routes
51 routeManager = sdk?.getRouteManager(locationManager, navigationManager: navigationManager)
52 // [swift_NavigineSdk_getRouteManager]
53
54 if let locationManager = locationManager,
55 let navigationManager = navigationManager,
56 let routeManager = routeManager {
57 print("LocationManager, NavigationManager and RouteManager successfully initialized")
58 }
59 } catch {
60 print("Error initializing SDK: \‍(error)")
61 }
62 }
63
64 /**
65 * Setup route listener
66 */
67 private func setupRouteListener() {
68 // RouteListener is implemented via protocol extension
69 // No additional setup needed for Swift
70 }
71
72 /**
73 * Demonstrate RouteManager methods
74 */
75 func demonstrateRouteManagerMethods() {
76 guard let manager = routeManager else {
77 print("RouteManager not initialized")
78 return
79 }
80
81 // [swift_RouteManager_addRouteListener]
82 // Add route listener
83 manager.addRouteListener(self)
84 // [swift_RouteManager_addRouteListener]
85
86 // [swift_RouteManager_setGraphTag]
87 // Set graph tag
88 manager.setGraphTag("main")
89 // [swift_RouteManager_setGraphTag]
90
91 // [swift_RouteManager_getGraphTag]
92 // Get current graph tag
93 let currentGraphTag = manager.getGraphTag()
94 print("Current graph tag: \‍(currentGraphTag)")
95 // [swift_RouteManager_getGraphTag]
96
97 // [swift_RouteManager_getGraphTags]
98 // Get all graph tags
99 let graphTags = manager.getGraphTags()
100 print("Available graph tags: \‍(graphTags)")
101 // [swift_RouteManager_getGraphTags]
102
103 // Create test points
104 let startPoint = NCPoint(x: 10.0, y: 20.0)
105 let endPoint = NCPoint(x: 50.0, y: 60.0)
106 let startLocationPoint = NCLocationPoint(point: startPoint, locationId: 12345, sublocationId: 1)
107 let endLocationPoint = NCLocationPoint(point: endPoint, locationId: 12345, sublocationId: 1)
108
109 // [swift_RouteManager_makeRoute]
110 // Make route from point to point
111 let routePath = manager.makeRoute(startLocationPoint, to: endLocationPoint)
112 print("Route created with length: \‍(routePath.getLength()) meters")
113 // [swift_RouteManager_makeRoute]
114
115 // [swift_RouteManager_makeRoutes]
116 // Make routes to multiple destinations
117 let destinations = [
118 NCLocationPoint(point: NCPoint(x: 30.0, y: 40.0), locationId: 12345, sublocationId: 1),
119 NCLocationPoint(point: NCPoint(x: 70.0, y: 80.0), locationId: 12345, sublocationId: 1)
120 ]
121 let routePaths = manager.makeRoutes(startLocationPoint, to: destinations)
122 print("Created \‍(routePaths.count) routes")
123 // [swift_RouteManager_makeRoutes]
124
125 // [swift_RouteManager_setTarget]
126 // Set target point
127 manager.setTarget(endLocationPoint)
128 // [swift_RouteManager_setTarget]
129
130 // [swift_RouteManager_addTarget]
131 // Add additional target point
132 let additionalTarget = NCLocationPoint(point: NCPoint(x: 90.0, y: 100.0), locationId: 12345, sublocationId: 1)
133 manager.addTarget(additionalTarget)
134 // [swift_RouteManager_addTarget]
135
136 // [swift_RouteManager_cancelTarget]
137 // Cancel current target
138 manager.cancelTarget()
139 // [swift_RouteManager_cancelTarget]
140
141 // [swift_RouteManager_clearTargets]
142 // Clear all targets
143 manager.clearTargets()
144 // [swift_RouteManager_clearTargets]
145 }
146
147 /**
148 * Demonstrate RoutePath class methods
149 */
150 func demonstrateRoutePathUsage(_ paths: [NCRoutePath]) {
151 for (index, path) in paths.enumerated() {
152 print("=== Route Path \‍(index + 1) ===")
153
154 // [swift_RoutePath_getLength]
155 // Get route length
156 let length = path.getLength()
157 print("Route length: \‍(length) meters")
158 // [swift_RoutePath_getLength]
159
160 // [swift_RoutePath_getPoints]
161 // Get route points
162 let points = path.getPoints()
163 print("Route has \‍(points.count) points")
164 for point in points {
165 demonstrateLocationPointUsage(point)
166 }
167 // [swift_RoutePath_getPoints]
168
169 // [swift_RoutePath_getEvents]
170 // Get route events
171 let events = path.getEvents()
172 print("Route has \‍(events.count) events")
173 for event in events {
174 demonstrateRouteEventUsage(event)
175 }
176 // [swift_RoutePath_getEvents]
177
178 // [swift_RoutePath_head]
179 // Get head of route (first 10 meters)
180 if let headPath = path.head(10.0) {
181 print("Head path length: \‍(headPath.getLength()) meters")
182 }
183 // [swift_RoutePath_head]
184
185 // [swift_RoutePath_tail]
186 // Get tail of route (remaining after 10 meters)
187 if let tailPath = path.tail(10.0) {
188 print("Tail path length: \‍(tailPath.getLength()) meters")
189 }
190 // [swift_RoutePath_tail]
191 }
192 }
193
194 /**
195 * Demonstrate RouteEvent class methods
196 */
197 func demonstrateRouteEventUsage(_ event: NCRouteEvent) {
198 // [swift_RouteEvent_getType]
199 // Get event type
200 let type = event.getType()
201 print("Event type: \‍(type)")
202 // [swift_RouteEvent_getType]
203
204 // [swift_RouteEvent_getValue]
205 // Get event value (angle for turns, sublocation ID for transitions)
206 let value = event.getValue()
207 print("Event value: \‍(value)")
208 // [swift_RouteEvent_getValue]
209
210 // [swift_RouteEvent_getDistance]
211 // Get distance from route start
212 let distance = event.getDistance()
213 print("Event distance: \‍(distance) meters")
214 // [swift_RouteEvent_getDistance]
215
216 // [swift_RouteEvent_constructor]
217 // Create new RouteEvent
218 let newEvent = NCRouteEvent(type: type, value: value, distance: distance)
219 print("Created new RouteEvent: \‍(newEvent)")
220 // [swift_RouteEvent_constructor]
221 }
222
223 /**
224 * Demonstrate RouteEventType enum values
225 */
226 func demonstrateRouteEventTypes() {
227 // [swift_RouteEventType_values]
228 // Get all route event type values
229 let types: [NCRouteEventType] = [.turnLeft, .turnRight, .transition]
230 print("Available route event types:")
231 for type in types {
232 print(" - \‍(type)")
233 }
234 // [swift_RouteEventType_values]
235 }
236
237 /**
238 * Demonstrate LocationPoint class methods
239 */
240 func demonstrateLocationPointUsage(_ locationPoint: NCLocationPoint) {
241 // [swift_LocationPoint_getPoint]
242 // Get point coordinates
243 if let point = locationPoint.getPoint() {
244 demonstratePointUsage(point)
245 }
246 // [swift_LocationPoint_getPoint]
247
248 // [swift_LocationPoint_getLocationId]
249 // Get location ID
250 let locationId = locationPoint.getLocationId()
251 print("Location ID: \‍(locationId)")
252 // [swift_LocationPoint_getLocationId]
253
254 // [swift_LocationPoint_getSublocationId]
255 // Get sublocation ID
256 let sublocationId = locationPoint.getSublocationId()
257 print("Sublocation ID: \‍(sublocationId)")
258 // [swift_LocationPoint_getSublocationId]
259
260 // [swift_LocationPoint_constructor]
261 // Create new LocationPoint
262 let newPoint = NCPoint(x: 15.0, y: 25.0)
263 let newLocationPoint = NCLocationPoint(point: newPoint, locationId: locationId, sublocationId: sublocationId)
264 print("Created new LocationPoint: \‍(newLocationPoint)")
265 // [swift_LocationPoint_constructor]
266 }
267
268 /**
269 * Demonstrate Point class methods
270 */
271 func demonstratePointUsage(_ point: NCPoint) {
272 // [swift_Point_getX]
273 // Get X coordinate
274 let x = point.getX()
275 print("Point X: \‍(x)")
276 // [swift_Point_getX]
277
278 // [swift_Point_getY]
279 // Get Y coordinate
280 let y = point.getY()
281 print("Point Y: \‍(y)")
282 // [swift_Point_getY]
283
284 // [swift_Point_constructor]
285 // Create new Point
286 let newPoint = NCPoint(x: x, y: y)
287 print("Created new Point: \‍(newPoint)")
288 // [swift_Point_constructor]
289 }
290
291 /**
292 * Demonstrate advanced routing features
293 */
294 func demonstrateAdvancedRoutingFeatures() {
295 print("=== Advanced Routing Features ===")
296
297 guard let manager = routeManager else {
298 return
299 }
300
301 // Create multiple routes with different graph tags
302 let graphTags = ["main", "elevator", "stairs"]
303
304 for tag in graphTags {
305 // [swift_RouteManager_setGraphTag_2]
306 // Set different graph tag for routing
307 manager.setGraphTag(tag)
308 // [swift_RouteManager_setGraphTag_2]
309
310 print("Routing with graph tag: \‍(tag)")
311
312 // Create test route
313 let startPoint = NCPoint(x: 0.0, y: 0.0)
314 let endPoint = NCPoint(x: 100.0, y: 100.0)
315 let startLocationPoint = NCLocationPoint(point: startPoint, locationId: 12345, sublocationId: 1)
316 let endLocationPoint = NCLocationPoint(point: endPoint, locationId: 12345, sublocationId: 1)
317
318 // [swift_RouteManager_makeRoute_2]
319 // Make route with specific graph tag
320 let routePath = manager.makeRoute(startLocationPoint, to: endLocationPoint)
321 // [swift_RouteManager_makeRoute_2]
322
323 print("Route with tag '\‍(tag)' has length: \‍(routePath.getLength()) meters")
324 }
325 }
326
327 /**
328 * Demonstrate route planning simulation
329 */
330 func demonstrateRoutePlanningSimulation() {
331 print("=== Route Planning Simulation ===")
332
333 guard let manager = routeManager else {
334 return
335 }
336
337 // Create multiple target points
338 let targets = [
339 NCLocationPoint(point: NCPoint(x: 50.0, y: 50.0), locationId: 12345, sublocationId: 1),
340 NCLocationPoint(point: NCPoint(x: 100.0, y: 100.0), locationId: 12345, sublocationId: 1),
341 NCLocationPoint(point: NCPoint(x: 150.0, y: 150.0), locationId: 12345, sublocationId: 1)
342 ]
343
344 // [swift_RouteManager_setTarget_2]
345 // Set first target
346 manager.setTarget(targets[0])
347 // [swift_RouteManager_setTarget_2]
348
349 print("Set target 1: \‍(targets[0])")
350 Thread.sleep(forTimeInterval: 1.0)
351
352 // [swift_RouteManager_addTarget_2]
353 // Add additional targets
354 for (index, target) in targets.dropFirst().enumerated() {
355 manager.addTarget(target)
356 print("Added target \‍(index + 2): \‍(target)")
357 Thread.sleep(forTimeInterval: 1.0)
358 }
359 // [swift_RouteManager_addTarget_2]
360
361 // [swift_RouteManager_cancelTarget_2]
362 // Cancel current target
363 manager.cancelTarget()
364 print("Cancelled current target")
365 // [swift_RouteManager_cancelTarget_2]
366
367 Thread.sleep(forTimeInterval: 1.0)
368
369 // [swift_RouteManager_clearTargets_2]
370 // Clear all targets
371 manager.clearTargets()
372 print("Cleared all targets")
373 // [swift_RouteManager_clearTargets_2]
374 }
375
376 /**
377 * Clean up resources
378 */
379 func cleanup() {
380 if let manager = routeManager {
381 // [swift_RouteManager_removeRouteListener]
382 // Remove route listener
383 manager.removeRouteListener(self)
384 // [swift_RouteManager_removeRouteListener]
385 }
386 }
387
388 /**
389 * Main demonstration method
390 */
391 func runExample() {
392 print("=== RouteManager Example ===")
393
394 demonstrateRouteManagerMethods()
395 demonstrateRouteEventTypes()
396 demonstrateAdvancedRoutingFeatures()
397 demonstrateRoutePlanningSimulation()
398
399 // Wait a bit for route updates
400 Thread.sleep(forTimeInterval: 3.0)
401
402 cleanup()
403 print("=== Example completed ===")
404 }
405}
406
407// MARK: - NCRouteListener
408
409extension RouteManagerExample: NCRouteListener {
410 // [swift_RouteListener_onPathsUpdated]
411 func onPathsUpdated(_ paths: [NCRoutePath]) {
412 print("Routes updated successfully")
413 demonstrateRoutePathUsage(paths)
414 }
415 // [swift_RouteListener_onPathsUpdated]
416}
417
418/**
419 * Function to run the example
420 */
421func main() {
422 let example = RouteManagerExample()
423 example.runExample()
424}
425
426// Run the example
427main()