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(2.0, 5.0, 2.0);
116
117 // [dart_AsyncRouteManager_createRouteSession]
118 // Create route session with default graph tag
119 RouteSession session = _asyncRouteManager!.createRouteSession(
120 destinationLocationPoint,
121 routeOptions,
122 );
123 print("Created route session with default graph tag");
124 // [dart_AsyncRouteManager_createRouteSession]
125
126 // [dart_AsyncRouteManager_createRouteSessionWithTag]
127 // Create route session with specific graph tag
128 RouteSession sessionWithTag = _asyncRouteManager!.createRouteSessionWithTag(
129 destinationLocationPoint,
130 routeOptions,
131 "main",
132 );
133 print("Created route session with 'main' graph tag");
134 // [dart_AsyncRouteManager_createRouteSessionWithTag]
135
136 // Store session for later use
137 _currentSession = session;
138
139 // Add listener to session
140 if (_asyncRouteListener != null) {
141 // [dart_RouteSession_addRouteListener]
142 // Add route listener to session
144 // [dart_RouteSession_addRouteListener]
145 }
146 }
147
151 void _demonstrateRoutePathUsage(RoutePath routePath) {
152 if (routePath == null) {
153 print("RoutePath is null");
154 return;
155 }
156
157 // [dart_RoutePath_getLength]
158 // Get route length
159 double length = routePath.length;
160 print("Route length: $length meters");
161 // [dart_RoutePath_getLength]
162
163 // [dart_RoutePath_getWeight]
164 // Get total route weight
165 double weight = routePath.weight;
166 print("Route weight: $weight");
167 // [dart_RoutePath_getWeight]
168
169 // [dart_RoutePath_getNodes]
170 // Get route nodes
171 List<RouteNode> nodes = routePath.nodes;
172 print("Number of route nodes: ${nodes.length}");
173 for (int i = 0; i < nodes.length && i < 3; i++) {
174 _demonstrateRouteNodeUsage(nodes[i]);
175 }
176 // [dart_RoutePath_getNodes]
177
178 // [dart_RoutePath_head]
179 // Get route head (first 10 meters)
180 RoutePath? head = routePath.head(10.0);
181 if (head != null) {
182 print("Route head length: ${head.length} meters");
183 }
184 // [dart_RoutePath_head]
185
186 // [dart_RoutePath_tail]
187 // Get route tail (remaining after 10 meters)
188 RoutePath? tail = routePath.tail(10.0);
189 if (tail != null) {
190 print("Route tail length: ${tail.length} meters");
191 }
192 // [dart_RoutePath_tail]
193 }
194
195 void _demonstrateRouteNodeUsage(RouteNode node) {
196 // [dart_RouteNode_getPoint]
197 LocationPoint point = node.point;
198 _demonstrateLocationPointUsage(point);
199 // [dart_RouteNode_getPoint]
200
201 // [dart_RouteNode_getWeight]
202 double weight = node.weight;
203 print("Node weight: $weight");
204 // [dart_RouteNode_getWeight]
205
206 // [dart_RouteNode_getDistance]
207 double distance = node.distance;
208 print("Node distance: $distance meters");
209 // [dart_RouteNode_getDistance]
210
211 // [dart_RouteNode_getEvents]
212 List<RouteEvent> events = node.events;
213 for (final event in events) {
214 _demonstrateRouteEventUsage(event);
215 }
216 // [dart_RouteNode_getEvents]
217 }
218
222 void _demonstrateRouteEventUsage(RouteEvent event) {
223 if (event == null) {
224 print("RouteEvent is null");
225 return;
226 }
227
228 // [dart_RouteEvent_getType]
229 // Get event type
230 RouteEventType type = event.type;
231 print("Event type: $type");
232 // [dart_RouteEvent_getType]
233
234 // [dart_RouteEvent_getTurnEvent]
235 TurnEvent? turnEvent = event.turnEvent;
236 if (turnEvent != null) {
237 _demonstrateTurnEventUsage(turnEvent);
238 }
239 // [dart_RouteEvent_getTurnEvent]
240
241 // [dart_RouteEvent_getTransitionEntryEvent]
242 TransitionEntryEvent? entryEvent = event.transitionEntryEvent;
243 if (entryEvent != null) {
244 _demonstrateTransitionEntryEventUsage(entryEvent);
245 }
246 // [dart_RouteEvent_getTransitionEntryEvent]
247
248 // [dart_RouteEvent_getTransitionExitEvent]
249 TransitionExitEvent? exitEvent = event.transitionExitEvent;
250 if (exitEvent != null) {
251 _demonstrateTransitionExitEventUsage(exitEvent);
252 }
253 // [dart_RouteEvent_getTransitionExitEvent]
254
255 // [dart_RouteEvent_getTargetReachedEvent]
256 TargetReachedEvent? reachedEvent = event.targetReachedEvent;
257 if (reachedEvent != null) {
258 _demonstrateTargetReachedEventUsage(reachedEvent);
259 }
260 // [dart_RouteEvent_getTargetReachedEvent]
261 }
262
267 // [dart_RouteEventType_values]
268 // Get all route event type values
269 print("Available route event types:");
270 print(" - RouteEventType.TURN_EVENT: ${RouteEventType.TURN_EVENT}");
271 print(" - RouteEventType.TRANSITION_ENTRY_EVENT: ${RouteEventType.TRANSITION_ENTRY_EVENT}");
272 print(" - RouteEventType.TRANSITION_EXIT_EVENT: ${RouteEventType.TRANSITION_EXIT_EVENT}");
273 print(" - RouteEventType.TARGET_REACHED_EVENT: ${RouteEventType.TARGET_REACHED_EVENT}");
274 // [dart_RouteEventType_values]
275 }
276
277 void demonstrateTurnTypes() {
278 // [dart_TurnType_values]
279 for (final type in TurnType.values) {
280 print(" - $type");
281 }
282 // [dart_TurnType_values]
283 }
284
285 void _demonstrateTurnEventUsage(TurnEvent event) {
286 // [dart_TurnEvent_getType]
287 TurnType type = event.type;
288 print("Turn type: $type");
289 // [dart_TurnEvent_getType]
290
291 // [dart_TurnEvent_getAngle]
292 int angle = event.angle;
293 print("Turn angle: $angle");
294 // [dart_TurnEvent_getAngle]
295 }
296
297 void _demonstrateTransitionEntryEventUsage(TransitionEntryEvent event) {
298 // [dart_TransitionEntryEvent_getFrom]
299 int from = event.from;
300 print("Transition entry from: $from");
301 // [dart_TransitionEntryEvent_getFrom]
302
303 // [dart_TransitionEntryEvent_getTo]
304 int to = event.to;
305 print("Transition entry to: $to");
306 // [dart_TransitionEntryEvent_getTo]
307 }
308
309 void _demonstrateTransitionExitEventUsage(TransitionExitEvent event) {
310 // [dart_TransitionExitEvent_getFrom]
311 int from = event.from;
312 print("Transition exit from: $from");
313 // [dart_TransitionExitEvent_getFrom]
314
315 // [dart_TransitionExitEvent_getTo]
316 int to = event.to;
317 print("Transition exit to: $to");
318 // [dart_TransitionExitEvent_getTo]
319 }
320
321 void _demonstrateTargetReachedEventUsage(TargetReachedEvent event) {
322 // [dart_TargetReachedEvent_getIndex]
323 int index = event.index;
324 print("Reached target index: $index");
325 // [dart_TargetReachedEvent_getIndex]
326
327 // [dart_TargetReachedEvent_getPoint]
328 LocationPoint point = event.point;
329 _demonstrateLocationPointUsage(point);
330 // [dart_TargetReachedEvent_getPoint]
331 }
332
336 void _demonstrateLocationPointUsage(LocationPoint locationPoint) {
337 if (locationPoint == null) {
338 print("LocationPoint is null");
339 return;
340 }
341
342 // [dart_LocationPoint_getPoint]
343 // Get point coordinates
344 Point point = locationPoint.point;
345 print("Location point: (${point.x}, ${point.y})");
346 // [dart_LocationPoint_getPoint]
347
348 // [dart_LocationPoint_getLocationId]
349 // Get location ID
350 int locationId = locationPoint.locationId;
351 print("Location ID: $locationId");
352 // [dart_LocationPoint_getLocationId]
353
354 // [dart_LocationPoint_getSublocationId]
355 // Get sublocation ID
356 int sublocationId = locationPoint.sublocationId;
357 print("Sublocation ID: $sublocationId");
358 // [dart_LocationPoint_getSublocationId]
359
360 // [dart_LocationPoint_constructor]
361 // Create new location point
362 Point newPoint = Point(x: 25.0, y: 35.0);
363 LocationPoint newLocationPoint = LocationPoint(
364 point: newPoint,
365 locationId: 12345,
366 sublocationId: 2,
367 );
368 print(
369 "Created new location point: ${newLocationPoint.point.x}, ${newLocationPoint.point.y}",
370 );
371 // [dart_LocationPoint_constructor]
372 }
373
377 void _demonstratePointUsage(Point point) {
378 if (point == null) {
379 print("Point is null");
380 return;
381 }
382
383 // [dart_Point_getX]
384 // Get X coordinate
385 double x = point.x;
386 print("Point X: $x");
387 // [dart_Point_getX]
388
389 // [dart_Point_getY]
390 // Get Y coordinate
391 double y = point.y;
392 print("Point Y: $y");
393 // [dart_Point_getY]
394
395 // [dart_Point_constructor]
396 // Create new point
397 Point newPoint = Point(x: 15.0, y: 25.0);
398 print("Created new point: (${newPoint.x}, ${newPoint.y})");
399 // [dart_Point_constructor]
400 }
401
406 print("=== Route Options ===");
407
408 // [dart_RouteOptions_constructor]
409 // Create route options with custom parameters
410 RouteOptions routeOptions = RouteOptions(3.0, 7.0, 2.5);
411 print(
412 "Created route options with smoothRadius: ${routeOptions.smoothRadius}, maxProjectionDistance: ${routeOptions.maxProjectionDistance}, maxAdvance: ${routeOptions.maxAdvance}",
413 );
414 // [dart_RouteOptions_constructor]
415
416 // Demonstrate different route options configurations
417 List<RouteOptions> optionsList = [
418 RouteOptions(0.0, 5.0, 2.0), // No smoothing
419 RouteOptions(2.0, 10.0, 1.0), // High smoothing, high projection distance
420 RouteOptions(1.0, 3.0, 3.0), // Low smoothing, low projection distance
421 ];
422
423 for (int i = 0; i < optionsList.length; i++) {
424 RouteOptions options = optionsList[i];
425 print(
426 "Options ${i + 1}: smoothRadius=${options.smoothRadius}, maxProjectionDistance=${options.maxProjectionDistance}, maxAdvance=${options.maxAdvance}",
427 );
428 }
429 }
430
434 Future<void> demonstrateAdvancedAsyncRoutingFeatures() async {
435 print("=== Advanced Async Routing Features ===");
436
437 if (_asyncRouteManager == null) {
438 return;
439 }
440
441 // Create multiple destination points
442 List<LocationPoint> destinations = [
443 LocationPoint(
444 point: Point(x: 50.0, y: 50.0),
445 locationId: 12345,
446 sublocationId: 1,
447 ),
448 LocationPoint(
449 point: Point(x: 100.0, y: 100.0),
450 locationId: 12345,
451 sublocationId: 1,
452 ),
453 LocationPoint(
454 point: Point(x: 150.0, y: 150.0),
455 locationId: 12345,
456 sublocationId: 1,
457 ),
458 ];
459
460 // Create different route options for each destination
461 List<RouteOptions> optionsList = [
462 RouteOptions(2.0, 5.0, 2.0),
463 RouteOptions(3.0, 7.0, 1.5),
464 RouteOptions(1.0, 3.0, 2.5),
465 ];
466
467 // Create sessions for each destination
468 List<RouteSession> sessions = [];
469 for (int i = 0; i < destinations.length; i++) {
470 RouteSession session = _asyncRouteManager!.createRouteSession(
471 destinations[i],
472 optionsList[i],
473 );
474 sessions.add(session);
475 print(
476 "Created session ${i + 1} for destination: ${destinations[i].point.x}, ${destinations[i].point.y}",
477 );
478
479 if (_asyncRouteListener != null) {
481 }
482
483 await Future.delayed(Duration(seconds: 1));
484 }
485
486 // Wait for route updates
487 await Future.delayed(Duration(seconds: 3));
488
489 // Cancel all sessions
490 for (int i = 0; i < sessions.length; i++) {
491 // [dart_AsyncRouteManager_cancelRouteSession]
492 // Cancel route session
493 _asyncRouteManager!.cancelRouteSession(sessions[i]);
494 print("Cancelled session ${i + 1}");
495 // [dart_AsyncRouteManager_cancelRouteSession]
496
497 await Future.delayed(Duration(seconds: 1));
498 }
499 }
500
505 print("=== RouteStatus Enum Usage ===");
506
507 // [dart_RouteStatus_values]
508 // Demonstrate all RouteStatus values
509 print("Available RouteStatus values:");
510 List<RouteStatus> allStatuses = [
511 RouteStatus.missingGraph,
512 RouteStatus.missingPosition,
513 RouteStatus.missingRoute,
514 RouteStatus.missingProjection,
515 RouteStatus.newRoute,
516 ];
517 for (RouteStatus status in allStatuses) {
518 print(" - $status: ${status.toString()}");
519 }
520
521 // Demonstrate status checking
522 RouteStatus testStatus = RouteStatus.newRoute;
523 switch (testStatus) {
524 case RouteStatus.newRoute:
525 print("Router is ready for navigation");
526 break;
527 case RouteStatus.missingGraph:
528 print("Router is missing the route graph");
529 break;
530 case RouteStatus.missingPosition:
531 print("Router is missing the current position");
532 break;
533 case RouteStatus.missingProjection:
534 print("Current position is off the route graph");
535 break;
536 case RouteStatus.missingRoute:
537 print("Router unable to find the route to the destination point");
538 break;
539 }
540 // [dart_RouteStatus_values]
541 }
542
547 print("=== Route Session Management ===");
548
549 if (_asyncRouteManager == null || _asyncRouteListener == null) {
550 return;
551 }
552
553 // Create test destination
554 Point destinationPoint = Point(x: 75.0, y: 75.0);
555 LocationPoint destinationLocationPoint = LocationPoint(
556 point: destinationPoint,
557 locationId: 12345,
558 sublocationId: 1,
559 );
560 RouteOptions routeOptions = RouteOptions(2.0, 5.0, 2.0);
561
562 // Create session with default tag
563 RouteSession defaultSession = _asyncRouteManager!.createRouteSession(
564 destinationLocationPoint,
565 routeOptions,
566 );
567 print("Created session with default tag");
568
569 // Create session with specific tag
570 RouteSession taggedSession = _asyncRouteManager!.createRouteSessionWithTag(
571 destinationLocationPoint,
572 routeOptions,
573 "elevator",
574 );
575 print("Created session with 'elevator' tag");
576
577 // Add listeners to both sessions
578 defaultSession.addRouteListener(_asyncRouteListener!);
580
581 // Simulate some time passing
582 Future.delayed(Duration(seconds: 2), () {
583 // Remove listeners
584 // [dart_RouteSession_removeRouteListener1]
585 // Remove route listener from session
588 print("Removed listeners from sessions");
589 // [dart_RouteSession_removeRouteListener1]
590
591 // Cancel sessions
592 _asyncRouteManager!.cancelRouteSession(defaultSession);
593 _asyncRouteManager!.cancelRouteSession(taggedSession);
594 print("Cancelled both sessions");
595 });
596 }
597
601 void cleanup() {
602 if (_currentSession != null && _asyncRouteListener != null) {
603 // [dart_RouteSession_removeRouteListener]
604 // Remove route listener
605 _currentSession!.removeRouteListener(_asyncRouteListener!);
606 // [dart_RouteSession_removeRouteListener]
607 }
608
609 if (_asyncRouteManager != null && _currentSession != null) {
610 // [dart_AsyncRouteManager_cancelRouteSession_1]
611 // Cancel route session
612 _asyncRouteManager!.cancelRouteSession(_currentSession!);
613 // [dart_AsyncRouteManager_cancelRouteSession_1]
614 }
615 }
616
620 Future<void> runExample() async {
621 print("=== AsyncRouteManager Example ===");
622
629 await demonstrateAdvancedAsyncRoutingFeatures();
630
631 // Wait a bit for route updates
632 await Future.delayed(Duration(seconds: 3));
633
634 cleanup();
635 print("=== Example completed ===");
636 }
637}
638
642Future<void> main() async {
644 await example.runExample();
645}