Loading...
Searching...
No Matches
async_route_manager_example.dart
Go to the documentation of this file.
1import 'dart:async';
2import 'dart:io';
3import 'package:navigine_sdk/navigine_sdk.dart';
4
9class AsyncRouteManagerExample {
16
17 AsyncRouteManagerExample() {
18 _initializeSdk();
19 _setupAsyncRouteListener();
20 }
21
25 void _initializeSdk() {
26 try {
27 // [dart_NavigineSdk_getInstance]
28 // Get SDK instance
30 // [dart_NavigineSdk_getInstance]
31
32 // [dart_NavigineSdk_setUserHash]
33 // Set user hash
34 _sdk?.setUserHash("USER-HASH-HERE");
35 // [dart_NavigineSdk_setUserHash]
36
37 // [dart_NavigineSdk_setServer]
38 // Set server URL (optional)
39 _sdk?.setServer("https://custom.navigine.com");
40 // [dart_NavigineSdk_setServer]
41
42 // [dart_NavigineSdk_getLocationManager]
43 // Get LocationManager for working with locations
45 // [dart_NavigineSdk_getLocationManager]
46
47 // [dart_NavigineSdk_getNavigationManager]
48 // Get NavigationManager for working with navigation
50 // [dart_NavigineSdk_getNavigationManager]
51
52 // [dart_NavigineSdk_getAsyncRouteManager]
53 // Get AsyncRouteManager for working with async routes
57 );
58 // [dart_NavigineSdk_getAsyncRouteManager]
59
60 if (_locationManager != null &&
61 _navigationManager != null &&
62 _asyncRouteManager != null) {
63 print(
64 "LocationManager, NavigationManager and AsyncRouteManager successfully initialized",
65 );
66 }
67 } catch (e) {
68 print("Error initializing SDK: $e");
69 }
70 }
71
75 void _setupAsyncRouteListener() {
76 _asyncRouteListener = AsyncRouteListener(
77 // [dart_AsyncRouteListener_onRouteChanged]
78 onRouteChanged: (RouteStatus status, RoutePath currentPath) {
79 print("Route changed with status: $status");
80 if (status == RouteStatus.newRoute && currentPath != null) {
81 _demonstrateRoutePathUsage(currentPath);
82 } else {
83 print("Route not ready, status: $status");
84 }
85 },
86 // [dart_AsyncRouteListener_onRouteChanged]
87
88 // [dart_AsyncRouteListener_onRouteAdvanced]
89 onRouteAdvanced: (double distance, LocationPoint point) {
90 print("Route advanced: $distance meters");
91 _demonstrateLocationPointUsage(point);
92 },
93 // [dart_AsyncRouteListener_onRouteAdvanced]
94 );
95 }
96
101 if (_asyncRouteManager == null) {
102 print("AsyncRouteManager not initialized");
103 return;
104 }
105
106 // Create test destination point
107 Point destinationPoint = Point(x: 100.0, y: 100.0);
108 LocationPoint destinationLocationPoint = LocationPoint(
109 point: destinationPoint,
110 locationId: 12345,
111 sublocationId: 1,
112 );
113
114 // Create route options
115 RouteOptions routeOptions = RouteOptions(
116 smoothRadius: 2.0,
117 maxProjectionDistance: 5.0,
118 maxAdvance: 2.0,
119 );
120
121 // [dart_AsyncRouteManager_createRouteSession]
122 // Create route session with default graph tag
123 RouteSession session = _asyncRouteManager!.createRouteSession(
124 destinationLocationPoint,
125 routeOptions,
126 );
127 print("Created route session with default graph tag");
128 // [dart_AsyncRouteManager_createRouteSession]
129
130 // [dart_AsyncRouteManager_createRouteSessionWithTag]
131 // Create route session with specific graph tag
132 RouteSession sessionWithTag = _asyncRouteManager!.createRouteSessionWithTag(
133 destinationLocationPoint,
134 routeOptions,
135 "main",
136 );
137 print("Created route session with 'main' graph tag");
138 // [dart_AsyncRouteManager_createRouteSessionWithTag]
139
140 // Store session for later use
141 _currentSession = session;
142
143 // Add listener to session
144 if (_asyncRouteListener != null) {
145 // [dart_RouteSession_addRouteListener]
146 // Add route listener to session
148 // [dart_RouteSession_addRouteListener]
149 }
150 }
151
155 void _demonstrateRoutePathUsage(RoutePath routePath) {
156 if (routePath == null) {
157 print("RoutePath is null");
158 return;
159 }
160
161 // [dart_RoutePath_getLength]
162 // Get route length
163 double length = routePath.length;
164 print("Route length: $length meters");
165 // [dart_RoutePath_getLength]
166
167 // [dart_RoutePath_getWeight]
168 // Get total route weight
169 double weight = routePath.weight;
170 print("Route weight: $weight");
171 // [dart_RoutePath_getWeight]
172
173 // [dart_RoutePath_getNodes]
174 // Get route nodes
175 List<RouteNode> nodes = routePath.nodes;
176 print("Number of route nodes: ${nodes.length}");
177 for (int i = 0; i < nodes.length && i < 3; i++) {
178 _demonstrateRouteNodeUsage(nodes[i]);
179 }
180 // [dart_RoutePath_getNodes]
181
182 // [dart_RoutePath_head]
183 // Get route head (first 10 meters)
184 RoutePath? head = routePath.head(10.0);
185 if (head != null) {
186 print("Route head length: ${head.length} meters");
187 }
188 // [dart_RoutePath_head]
189
190 // [dart_RoutePath_tail]
191 // Get route tail (remaining after 10 meters)
192 RoutePath? tail = routePath.tail(10.0);
193 if (tail != null) {
194 print("Route tail length: ${tail.length} meters");
195 }
196 // [dart_RoutePath_tail]
197 }
198
199 void _demonstrateRouteNodeUsage(RouteNode node) {
200 // [dart_RouteNode_getPoint]
201 LocationPoint point = node.point;
202 _demonstrateLocationPointUsage(point);
203 // [dart_RouteNode_getPoint]
204
205 // [dart_RouteNode_getWeight]
206 double weight = node.weight;
207 print("Node weight: $weight");
208 // [dart_RouteNode_getWeight]
209
210 // [dart_RouteNode_getDistance]
211 double distance = node.distance;
212 print("Node distance: $distance meters");
213 // [dart_RouteNode_getDistance]
214
215 // [dart_RouteNode_getEvents]
216 List<RouteEvent> events = node.events;
217 for (final event in events) {
218 _demonstrateRouteEventUsage(event);
219 }
220 // [dart_RouteNode_getEvents]
221 }
222
226 void _demonstrateRouteEventUsage(RouteEvent event) {
227 if (event == null) {
228 print("RouteEvent is null");
229 return;
230 }
231
232 // [dart_RouteEvent_getType]
233 // Get event type
234 RouteEventType type = event.type;
235 print("Event type: $type");
236 // [dart_RouteEvent_getType]
237
238 // [dart_RouteEvent_getTurnEvent]
239 TurnEvent? turnEvent = event.turnEvent;
240 if (turnEvent != null) {
241 _demonstrateTurnEventUsage(turnEvent);
242 }
243 // [dart_RouteEvent_getTurnEvent]
244
245 // [dart_RouteEvent_getTransitionEntryEvent]
246 TransitionEntryEvent? entryEvent = event.transitionEntryEvent;
247 if (entryEvent != null) {
248 _demonstrateTransitionEntryEventUsage(entryEvent);
249 }
250 // [dart_RouteEvent_getTransitionEntryEvent]
251
252 // [dart_RouteEvent_getTransitionExitEvent]
253 TransitionExitEvent? exitEvent = event.transitionExitEvent;
254 if (exitEvent != null) {
255 _demonstrateTransitionExitEventUsage(exitEvent);
256 }
257 // [dart_RouteEvent_getTransitionExitEvent]
258
259 // [dart_RouteEvent_getTargetReachedEvent]
260 TargetReachedEvent? reachedEvent = event.targetReachedEvent;
261 if (reachedEvent != null) {
262 _demonstrateTargetReachedEventUsage(reachedEvent);
263 }
264 // [dart_RouteEvent_getTargetReachedEvent]
265 }
266
271 // [dart_RouteEventType_values]
272 // Get all route event type values
273 print("Available route event types:");
274 print(" - RouteEventType.TURN_EVENT: ${RouteEventType.TURN_EVENT}");
275 print(" - RouteEventType.TRANSITION_ENTRY_EVENT: ${RouteEventType.TRANSITION_ENTRY_EVENT}");
276 print(" - RouteEventType.TRANSITION_EXIT_EVENT: ${RouteEventType.TRANSITION_EXIT_EVENT}");
277 print(" - RouteEventType.TARGET_REACHED_EVENT: ${RouteEventType.TARGET_REACHED_EVENT}");
278 // [dart_RouteEventType_values]
279 }
280
281 void demonstrateTurnTypes() {
282 // [dart_TurnType_values]
283 for (final type in TurnType.values) {
284 print(" - $type");
285 }
286 // [dart_TurnType_values]
287 }
288
289 void _demonstrateTurnEventUsage(TurnEvent event) {
290 // [dart_TurnEvent_getType]
291 TurnType type = event.type;
292 print("Turn type: $type");
293 // [dart_TurnEvent_getType]
294
295 // [dart_TurnEvent_getAngle]
296 int angle = event.angle;
297 print("Turn angle: $angle");
298 // [dart_TurnEvent_getAngle]
299 }
300
301 void _demonstrateTransitionEntryEventUsage(TransitionEntryEvent event) {
302 // [dart_TransitionEntryEvent_getFrom]
303 int from = event.from;
304 print("Transition entry from: $from");
305 // [dart_TransitionEntryEvent_getFrom]
306
307 // [dart_TransitionEntryEvent_getTo]
308 int to = event.to;
309 print("Transition entry to: $to");
310 // [dart_TransitionEntryEvent_getTo]
311 }
312
313 void _demonstrateTransitionExitEventUsage(TransitionExitEvent event) {
314 // [dart_TransitionExitEvent_getFrom]
315 int from = event.from;
316 print("Transition exit from: $from");
317 // [dart_TransitionExitEvent_getFrom]
318
319 // [dart_TransitionExitEvent_getTo]
320 int to = event.to;
321 print("Transition exit to: $to");
322 // [dart_TransitionExitEvent_getTo]
323 }
324
325 void _demonstrateTargetReachedEventUsage(TargetReachedEvent event) {
326 // [dart_TargetReachedEvent_getIndex]
327 int index = event.index;
328 print("Reached target index: $index");
329 // [dart_TargetReachedEvent_getIndex]
330
331 // [dart_TargetReachedEvent_getPoint]
332 LocationPoint point = event.point;
333 _demonstrateLocationPointUsage(point);
334 // [dart_TargetReachedEvent_getPoint]
335 }
336
340 void _demonstrateLocationPointUsage(LocationPoint locationPoint) {
341 if (locationPoint == null) {
342 print("LocationPoint is null");
343 return;
344 }
345
346 // [dart_LocationPoint_getPoint]
347 // Get point coordinates
348 Point point = locationPoint.point;
349 print("Location point: (${point.x}, ${point.y})");
350 // [dart_LocationPoint_getPoint]
351
352 // [dart_LocationPoint_getLocationId]
353 // Get location ID
354 int locationId = locationPoint.locationId;
355 print("Location ID: $locationId");
356 // [dart_LocationPoint_getLocationId]
357
358 // [dart_LocationPoint_getSublocationId]
359 // Get sublocation ID
360 int sublocationId = locationPoint.sublocationId;
361 print("Sublocation ID: $sublocationId");
362 // [dart_LocationPoint_getSublocationId]
363
364 // [dart_LocationPoint_constructor]
365 // Create new location point
366 Point newPoint = Point(x: 25.0, y: 35.0);
367 LocationPoint newLocationPoint = LocationPoint(
368 point: newPoint,
369 locationId: 12345,
370 sublocationId: 2,
371 );
372 print(
373 "Created new location point: ${newLocationPoint.point.x}, ${newLocationPoint.point.y}",
374 );
375 // [dart_LocationPoint_constructor]
376 }
377
381 void _demonstratePointUsage(Point point) {
382 if (point == null) {
383 print("Point is null");
384 return;
385 }
386
387 // [dart_Point_getX]
388 // Get X coordinate
389 double x = point.x;
390 print("Point X: $x");
391 // [dart_Point_getX]
392
393 // [dart_Point_getY]
394 // Get Y coordinate
395 double y = point.y;
396 print("Point Y: $y");
397 // [dart_Point_getY]
398
399 // [dart_Point_constructor]
400 // Create new point
401 Point newPoint = Point(x: 15.0, y: 25.0);
402 print("Created new point: (${newPoint.x}, ${newPoint.y})");
403 // [dart_Point_constructor]
404 }
405
410 print("=== Route Options ===");
411
412 // [dart_RouteOptions_constructor]
413 // Create route options (named params; omitted fields use IDL defaults).
414 RouteOptions routeOptions = RouteOptions(
415 smoothRadius: 3.0,
416 maxProjectionDistance: 7.0,
417 maxAdvance: 2.5,
418 );
419 print(
420 "Created route options with smoothRadius: ${routeOptions.smoothRadius}, maxProjectionDistance: ${routeOptions.maxProjectionDistance}, maxAdvance: ${routeOptions.maxAdvance}",
421 );
422 // [dart_RouteOptions_constructor]
423
424 // Demonstrate different route options configurations
425 List<RouteOptions> optionsList = [
426 RouteOptions(), // IDL defaults: 0 / 5 / 2
427 RouteOptions(smoothRadius: 2.0, maxProjectionDistance: 10.0, maxAdvance: 1.0),
428 RouteOptions(smoothRadius: 1.0, maxProjectionDistance: 3.0, maxAdvance: 3.0),
429 ];
430
431 for (int i = 0; i < optionsList.length; i++) {
432 RouteOptions options = optionsList[i];
433 print(
434 "Options ${i + 1}: smoothRadius=${options.smoothRadius}, maxProjectionDistance=${options.maxProjectionDistance}, maxAdvance=${options.maxAdvance}",
435 );
436 }
437 }
438
442 Future<void> demonstrateAdvancedAsyncRoutingFeatures() async {
443 print("=== Advanced Async Routing Features ===");
444
445 if (_asyncRouteManager == null) {
446 return;
447 }
448
449 // Create multiple destination points
450 List<LocationPoint> destinations = [
451 LocationPoint(
452 point: Point(x: 50.0, y: 50.0),
453 locationId: 12345,
454 sublocationId: 1,
455 ),
456 LocationPoint(
457 point: Point(x: 100.0, y: 100.0),
458 locationId: 12345,
459 sublocationId: 1,
460 ),
461 LocationPoint(
462 point: Point(x: 150.0, y: 150.0),
463 locationId: 12345,
464 sublocationId: 1,
465 ),
466 ];
467
468 // Create different route options for each destination
469 List<RouteOptions> optionsList = [
470 RouteOptions(smoothRadius: 2.0, maxProjectionDistance: 5.0, maxAdvance: 2.0),
471 RouteOptions(smoothRadius: 3.0, maxProjectionDistance: 7.0, maxAdvance: 1.5),
472 RouteOptions(smoothRadius: 1.0, maxProjectionDistance: 3.0, maxAdvance: 2.5),
473 ];
474
475 // Create sessions for each destination
476 List<RouteSession> sessions = [];
477 for (int i = 0; i < destinations.length; i++) {
478 RouteSession session = _asyncRouteManager!.createRouteSession(
479 destinations[i],
480 optionsList[i],
481 );
482 sessions.add(session);
483 print(
484 "Created session ${i + 1} for destination: ${destinations[i].point.x}, ${destinations[i].point.y}",
485 );
486
487 if (_asyncRouteListener != null) {
489 }
490
491 await Future.delayed(Duration(seconds: 1));
492 }
493
494 // Wait for route updates
495 await Future.delayed(Duration(seconds: 3));
496
497 // Cancel all sessions
498 for (int i = 0; i < sessions.length; i++) {
499 // [dart_AsyncRouteManager_cancelRouteSession]
500 // Cancel route session
501 _asyncRouteManager!.cancelRouteSession(sessions[i]);
502 print("Cancelled session ${i + 1}");
503 // [dart_AsyncRouteManager_cancelRouteSession]
504
505 await Future.delayed(Duration(seconds: 1));
506 }
507 }
508
513 print("=== RouteStatus Enum Usage ===");
514
515 // [dart_RouteStatus_values]
516 // Demonstrate all RouteStatus values
517 print("Available RouteStatus values:");
518 List<RouteStatus> allStatuses = [
519 RouteStatus.missingGraph,
520 RouteStatus.missingPosition,
521 RouteStatus.missingRoute,
522 RouteStatus.missingProjection,
523 RouteStatus.newRoute,
524 ];
525 for (RouteStatus status in allStatuses) {
526 print(" - $status: ${status.toString()}");
527 }
528
529 // Demonstrate status checking
530 RouteStatus testStatus = RouteStatus.newRoute;
531 switch (testStatus) {
532 case RouteStatus.newRoute:
533 print("Router is ready for navigation");
534 break;
535 case RouteStatus.missingGraph:
536 print("Router is missing the route graph");
537 break;
538 case RouteStatus.missingPosition:
539 print("Router is missing the current position");
540 break;
541 case RouteStatus.missingProjection:
542 print("Current position is off the route graph");
543 break;
544 case RouteStatus.missingRoute:
545 print("Router unable to find the route to the destination point");
546 break;
547 }
548 // [dart_RouteStatus_values]
549 }
550
555 print("=== Route Session Management ===");
556
557 if (_asyncRouteManager == null || _asyncRouteListener == null) {
558 return;
559 }
560
561 // Create test destination
562 Point destinationPoint = Point(x: 75.0, y: 75.0);
563 LocationPoint destinationLocationPoint = LocationPoint(
564 point: destinationPoint,
565 locationId: 12345,
566 sublocationId: 1,
567 );
568 RouteOptions routeOptions = RouteOptions(
569 smoothRadius: 2.0,
570 maxProjectionDistance: 5.0,
571 maxAdvance: 2.0,
572 );
573
574 // Create session with default tag
575 RouteSession defaultSession = _asyncRouteManager!.createRouteSession(
576 destinationLocationPoint,
577 routeOptions,
578 );
579 print("Created session with default tag");
580
581 // Create session with specific tag
582 RouteSession taggedSession = _asyncRouteManager!.createRouteSessionWithTag(
583 destinationLocationPoint,
584 routeOptions,
585 "elevator",
586 );
587 print("Created session with 'elevator' tag");
588
589 // Add listeners to both sessions
590 defaultSession.addRouteListener(_asyncRouteListener!);
592
593 // Simulate some time passing
594 Future.delayed(Duration(seconds: 2), () {
595 // Remove listeners
596 // [dart_RouteSession_removeRouteListener1]
597 // Remove route listener from session
600 print("Removed listeners from sessions");
601 // [dart_RouteSession_removeRouteListener1]
602
603 // Cancel sessions
604 _asyncRouteManager!.cancelRouteSession(defaultSession);
605 _asyncRouteManager!.cancelRouteSession(taggedSession);
606 print("Cancelled both sessions");
607 });
608 }
609
613 void cleanup() {
614 if (_currentSession != null && _asyncRouteListener != null) {
615 // [dart_RouteSession_removeRouteListener]
616 // Remove route listener
617 _currentSession!.removeRouteListener(_asyncRouteListener!);
618 // [dart_RouteSession_removeRouteListener]
619 }
620
621 if (_asyncRouteManager != null && _currentSession != null) {
622 // [dart_AsyncRouteManager_cancelRouteSession_1]
623 // Cancel route session
624 _asyncRouteManager!.cancelRouteSession(_currentSession!);
625 // [dart_AsyncRouteManager_cancelRouteSession_1]
626 }
627 }
628
632 Future<void> runExample() async {
633 print("=== AsyncRouteManager Example ===");
634
641 await demonstrateAdvancedAsyncRoutingFeatures();
642
643 // Wait a bit for route updates
644 await Future.delayed(Duration(seconds: 3));
645
646 cleanup();
647 print("=== Example completed ===");
648 }
649}
650
654Future<void> main() async {
656 await example.runExample();
657}