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 {
10 NavigineSdk? _sdk;
11 LocationManager? _locationManager;
12 NavigationManager? _navigationManager;
13 AsyncRouteManager? _asyncRouteManager;
14 AsyncRouteListener? _asyncRouteListener;
15 RouteSession? _currentSession;
16
17 AsyncRouteManagerExample() {
18 _initializeSdk();
19 _setupAsyncRouteListener();
20 }
21
25 void _initializeSdk() {
26 try {
27 // [dart_NavigineSdk_getInstance]
28 // Get SDK instance
29 _sdk = NavigineSdk.getInstance();
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
44 _locationManager = _sdk?.getLocationManager();
45 // [dart_NavigineSdk_getLocationManager]
46
47 // [dart_NavigineSdk_getNavigationManager]
48 // Get NavigationManager for working with navigation
49 _navigationManager = _sdk?.getNavigationManager(_locationManager!);
50 // [dart_NavigineSdk_getNavigationManager]
51
52 // [dart_NavigineSdk_getAsyncRouteManager]
53 // Get AsyncRouteManager for working with async routes
54 _asyncRouteManager = _sdk?.getAsyncRouteManager(
55 _locationManager!,
56 _navigationManager!,
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
100 void demonstrateAsyncRouteManagerMethods() {
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
143 session.addRouteListener(_asyncRouteListener!);
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_getPoints]
164 // Get route points
165 List<LocationPoint> points = routePath.points;
166 print("Number of route points: ${points.length}");
167 for (int i = 0; i < points.length && i < 3; i++) {
168 print("Point ${i + 1}: ${points[i]}");
169 }
170 // [dart_RoutePath_getPoints]
171
172 // [dart_RoutePath_getEvents]
173 // Get route events
174 List<RouteEvent> events = routePath.events;
175 print("Number of route events: ${events.length}");
176 for (int i = 0; i < events.length && i < 3; i++) {
177 _demonstrateRouteEventUsage(events[i]);
178 }
179 // [dart_RoutePath_getEvents]
180
181 // [dart_RoutePath_head]
182 // Get route head (first 10 meters)
183 RoutePath head = routePath.head(10.0);
184 if (head != null) {
185 print("Route head length: ${head.length} meters");
186 }
187 // [dart_RoutePath_head]
188
189 // [dart_RoutePath_tail]
190 // Get route tail (remaining after 10 meters)
191 RoutePath tail = routePath.tail(10.0);
192 if (tail != null) {
193 print("Route tail length: ${tail.length} meters");
194 }
195 // [dart_RoutePath_tail]
196 }
197
201 void _demonstrateRouteEventUsage(RouteEvent event) {
202 if (event == null) {
203 print("RouteEvent is null");
204 return;
205 }
206
207 // [dart_RouteEvent_getType]
208 // Get event type
209 RouteEventType type = event.type;
210 print("Event type: $type");
211 // [dart_RouteEvent_getType]
212
213 // [dart_RouteEvent_getValue]
214 // Get event value
215 int value = event.value;
216 print("Event value: $value");
217 // [dart_RouteEvent_getValue]
218
219 // [dart_RouteEvent_getDistance]
220 // Get event distance
221 double distance = event.distance;
222 print("Event distance: $distance meters");
223 // [dart_RouteEvent_getDistance]
224
225 // [dart_RouteEvent_constructor]
226 // Create new route event
227 RouteEvent newEvent = RouteEvent(RouteEventType.TURN_LEFT, 90, 50.0);
228 print(
229 "Created new route event: ${newEvent.type} at ${newEvent.distance} meters",
230 );
231 // [dart_RouteEvent_constructor]
232 }
233
237 void demonstrateRouteEventTypes() {
238 // [dart_RouteEventType_values]
239 // Get all route event type values
240 print("Available route event types:");
241 print(" - RouteEventType.TURN_LEFT: ${RouteEventType.TURN_LEFT}");
242 print(" - RouteEventType.TURN_RIGHT: ${RouteEventType.TURN_RIGHT}");
243 print(" - RouteEventType.TRANSITION: ${RouteEventType.TRANSITION}");
244 // [dart_RouteEventType_values]
245 }
246
250 void _demonstrateLocationPointUsage(LocationPoint locationPoint) {
251 if (locationPoint == null) {
252 print("LocationPoint is null");
253 return;
254 }
255
256 // [dart_LocationPoint_getPoint]
257 // Get point coordinates
258 Point point = locationPoint.point;
259 print("Location point: (${point.x}, ${point.y})");
260 // [dart_LocationPoint_getPoint]
261
262 // [dart_LocationPoint_getLocationId]
263 // Get location ID
264 int locationId = locationPoint.locationId;
265 print("Location ID: $locationId");
266 // [dart_LocationPoint_getLocationId]
267
268 // [dart_LocationPoint_getSublocationId]
269 // Get sublocation ID
270 int sublocationId = locationPoint.sublocationId;
271 print("Sublocation ID: $sublocationId");
272 // [dart_LocationPoint_getSublocationId]
273
274 // [dart_LocationPoint_constructor]
275 // Create new location point
276 Point newPoint = Point(x: 25.0, y: 35.0);
277 LocationPoint newLocationPoint = LocationPoint(
278 point: newPoint,
279 locationId: 12345,
280 sublocationId: 2,
281 );
282 print(
283 "Created new location point: ${newLocationPoint.point.x}, ${newLocationPoint.point.y}",
284 );
285 // [dart_LocationPoint_constructor]
286 }
287
291 void _demonstratePointUsage(Point point) {
292 if (point == null) {
293 print("Point is null");
294 return;
295 }
296
297 // [dart_Point_getX]
298 // Get X coordinate
299 double x = point.x;
300 print("Point X: $x");
301 // [dart_Point_getX]
302
303 // [dart_Point_getY]
304 // Get Y coordinate
305 double y = point.y;
306 print("Point Y: $y");
307 // [dart_Point_getY]
308
309 // [dart_Point_constructor]
310 // Create new point
311 Point newPoint = Point(x: 15.0, y: 25.0);
312 print("Created new point: (${newPoint.x}, ${newPoint.y})");
313 // [dart_Point_constructor]
314 }
315
319 void demonstrateRouteOptions() {
320 print("=== Route Options ===");
321
322 // [dart_RouteOptions_constructor]
323 // Create route options with custom parameters
324 RouteOptions routeOptions = RouteOptions(3.0, 7.0, 2.5);
325 print(
326 "Created route options with smoothRadius: ${routeOptions.smoothRadius}, maxProjectionDistance: ${routeOptions.maxProjectionDistance}, maxAdvance: ${routeOptions.maxAdvance}",
327 );
328 // [dart_RouteOptions_constructor]
329
330 // Demonstrate different route options configurations
331 List<RouteOptions> optionsList = [
332 RouteOptions(0.0, 5.0, 2.0), // No smoothing
333 RouteOptions(2.0, 10.0, 1.0), // High smoothing, high projection distance
334 RouteOptions(1.0, 3.0, 3.0), // Low smoothing, low projection distance
335 ];
336
337 for (int i = 0; i < optionsList.length; i++) {
338 RouteOptions options = optionsList[i];
339 print(
340 "Options ${i + 1}: smoothRadius=${options.smoothRadius}, maxProjectionDistance=${options.maxProjectionDistance}, maxAdvance=${options.maxAdvance}",
341 );
342 }
343 }
344
348 Future<void> demonstrateAdvancedAsyncRoutingFeatures() async {
349 print("=== Advanced Async Routing Features ===");
350
351 if (_asyncRouteManager == null) {
352 return;
353 }
354
355 // Create multiple destination points
356 List<LocationPoint> destinations = [
357 LocationPoint(
358 point: Point(x: 50.0, y: 50.0),
359 locationId: 12345,
360 sublocationId: 1,
361 ),
362 LocationPoint(
363 point: Point(x: 100.0, y: 100.0),
364 locationId: 12345,
365 sublocationId: 1,
366 ),
367 LocationPoint(
368 point: Point(x: 150.0, y: 150.0),
369 locationId: 12345,
370 sublocationId: 1,
371 ),
372 ];
373
374 // Create different route options for each destination
375 List<RouteOptions> optionsList = [
376 RouteOptions(2.0, 5.0, 2.0),
377 RouteOptions(3.0, 7.0, 1.5),
378 RouteOptions(1.0, 3.0, 2.5),
379 ];
380
381 // Create sessions for each destination
382 List<RouteSession> sessions = [];
383 for (int i = 0; i < destinations.length; i++) {
384 RouteSession session = _asyncRouteManager!.createRouteSession(
385 destinations[i],
386 optionsList[i],
387 );
388 sessions.add(session);
389 print(
390 "Created session ${i + 1} for destination: ${destinations[i].point.x}, ${destinations[i].point.y}",
391 );
392
393 if (_asyncRouteListener != null) {
394 session.addRouteListener(_asyncRouteListener!);
395 }
396
397 await Future.delayed(Duration(seconds: 1));
398 }
399
400 // Wait for route updates
401 await Future.delayed(Duration(seconds: 3));
402
403 // Cancel all sessions
404 for (int i = 0; i < sessions.length; i++) {
405 // [dart_AsyncRouteManager_cancelRouteSession]
406 // Cancel route session
407 _asyncRouteManager!.cancelRouteSession(sessions[i]);
408 print("Cancelled session ${i + 1}");
409 // [dart_AsyncRouteManager_cancelRouteSession]
410
411 await Future.delayed(Duration(seconds: 1));
412 }
413 }
414
418 void demonstrateRouteStatusUsage() {
419 print("=== RouteStatus Enum Usage ===");
420
421 // [dart_RouteStatus_values]
422 // Demonstrate all RouteStatus values
423 print("Available RouteStatus values:");
424 List<RouteStatus> allStatuses = [
425 RouteStatus.missingGraph,
426 RouteStatus.missingPosition,
427 RouteStatus.missingRoute,
428 RouteStatus.missingProjection,
429 RouteStatus.newRoute,
430 ];
431 for (RouteStatus status in allStatuses) {
432 print(" - $status: ${status.toString()}");
433 }
434
435 // Demonstrate status checking
436 RouteStatus testStatus = RouteStatus.newRoute;
437 switch (testStatus) {
438 case RouteStatus.newRoute:
439 print("Router is ready for navigation");
440 break;
441 case RouteStatus.missingGraph:
442 print("Router is missing the route graph");
443 break;
444 case RouteStatus.missingPosition:
445 print("Router is missing the current position");
446 break;
447 case RouteStatus.missingProjection:
448 print("Current position is off the route graph");
449 break;
450 case RouteStatus.missingRoute:
451 print("Router unable to find the route to the destination point");
452 break;
453 }
454 // [dart_RouteStatus_values]
455 }
456
460 void demonstrateRouteSessionManagement() {
461 print("=== Route Session Management ===");
462
463 if (_asyncRouteManager == null || _asyncRouteListener == null) {
464 return;
465 }
466
467 // Create test destination
468 Point destinationPoint = Point(x: 75.0, y: 75.0);
469 LocationPoint destinationLocationPoint = LocationPoint(
470 point: destinationPoint,
471 locationId: 12345,
472 sublocationId: 1,
473 );
474 RouteOptions routeOptions = RouteOptions(2.0, 5.0, 2.0);
475
476 // Create session with default tag
477 RouteSession defaultSession = _asyncRouteManager!.createRouteSession(
478 destinationLocationPoint,
479 routeOptions,
480 );
481 print("Created session with default tag");
482
483 // Create session with specific tag
484 RouteSession taggedSession = _asyncRouteManager!.createRouteSessionWithTag(
485 destinationLocationPoint,
486 routeOptions,
487 "elevator",
488 );
489 print("Created session with 'elevator' tag");
490
491 // Add listeners to both sessions
492 defaultSession.addRouteListener(_asyncRouteListener!);
493 taggedSession.addRouteListener(_asyncRouteListener!);
494
495 // Simulate some time passing
496 Future.delayed(Duration(seconds: 2), () {
497 // Remove listeners
498 // [dart_RouteSession_removeRouteListener]
499 // Remove route listener from session
500 defaultSession.removeRouteListener(_asyncRouteListener!);
501 taggedSession.removeRouteListener(_asyncRouteListener!);
502 print("Removed listeners from sessions");
503 // [dart_RouteSession_removeRouteListener]
504
505 // Cancel sessions
506 _asyncRouteManager!.cancelRouteSession(defaultSession);
507 _asyncRouteManager!.cancelRouteSession(taggedSession);
508 print("Cancelled both sessions");
509 });
510 }
511
515 void cleanup() {
516 if (_currentSession != null && _asyncRouteListener != null) {
517 // [dart_RouteSession_removeRouteListener]
518 // Remove route listener
519 _currentSession!.removeRouteListener(_asyncRouteListener!);
520 // [dart_RouteSession_removeRouteListener]
521 }
522
523 if (_asyncRouteManager != null && _currentSession != null) {
524 // [dart_AsyncRouteManager_cancelRouteSession_1]
525 // Cancel route session
526 _asyncRouteManager!.cancelRouteSession(_currentSession!);
527 // [dart_AsyncRouteManager_cancelRouteSession_1]
528 }
529 }
530
534 Future<void> runExample() async {
535 print("=== AsyncRouteManager Example ===");
536
537 demonstrateAsyncRouteManagerMethods();
538 demonstrateRouteEventTypes();
539 demonstrateRouteOptions();
540 demonstrateRouteStatusUsage();
541 demonstrateRouteSessionManagement();
542 await demonstrateAdvancedAsyncRoutingFeatures();
543
544 // Wait a bit for route updates
545 await Future.delayed(Duration(seconds: 3));
546
547 cleanup();
548 print("=== Example completed ===");
549 }
550}
551
555Future<void> main() async {
557 await example.runExample();
558}