Loading...
Searching...
No Matches
AsyncRouteManagerExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import java.util.List;
4import java.util.ArrayList;
5
11 private NavigineSdk sdk;
12 private LocationManager locationManager;
13 private NavigationManager navigationManager;
14 private AsyncRouteManager asyncRouteManager;
15 private AsyncRouteListener asyncRouteListener;
16 private RouteSession currentSession;
17
19 initializeSdk();
20 setupAsyncRouteListener();
21 }
22
26 private void initializeSdk() {
27 try {
28 // [java_NavigineSdk_getInstance]
29 // Get SDK instance
31 // [java_NavigineSdk_getInstance]
32
33 // [java_NavigineSdk_setUserHash]
34 // Set user hash
35 sdk.setUserHash("USER-HASH-HERE");
36 // [java_NavigineSdk_setUserHash]
37
38 // [java_NavigineSdk_setServer]
39 // Set server URL (optional)
40 sdk.setServer("https://custom.navigine.com");
41 // [java_NavigineSdk_setServer]
42
43 // [java_NavigineSdk_getLocationManager]
44 // Get LocationManager for working with locations
45 locationManager = sdk.getLocationManager();
46 // [java_NavigineSdk_getLocationManager]
47
48 // [java_NavigineSdk_getNavigationManager]
49 // Get NavigationManager for working with navigation
50 navigationManager = sdk.getNavigationManager(locationManager);
51 // [java_NavigineSdk_getNavigationManager]
52
53 // [java_NavigineSdk_getAsyncRouteManager]
54 // Get AsyncRouteManager for working with async routes
55 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager);
56 // [java_NavigineSdk_getAsyncRouteManager]
57
58 if (locationManager != null && navigationManager != null && asyncRouteManager != null) {
59 System.out.println("LocationManager, NavigationManager and AsyncRouteManager successfully initialized");
60 }
61 } catch (Exception e) {
62 System.err.println("Error initializing SDK: " + e.getMessage());
63 }
64 }
65
69 private void setupAsyncRouteListener() {
70 asyncRouteListener = new AsyncRouteListener() {
71 @Override
72 // [java_AsyncRouteListener_onRouteChanged]
73 public void onRouteChanged(RouteStatus status, RoutePath currentPath) {
74 System.out.println("Route changed with status: " + status);
75 if (status == RouteStatus.NEW_ROUTE && currentPath != null) {
76 demonstrateRoutePathUsage(currentPath);
77 } else {
78 System.out.println("Route not ready, status: " + status);
79 }
80 }
81 // [java_AsyncRouteListener_onRouteChanged]
82
83 @Override
84 // [java_AsyncRouteListener_onRouteAdvanced]
85 public void onRouteAdvanced(float distance, LocationPoint point) {
86 System.out.println("Route advanced: " + distance + " meters");
88 }
89 // [java_AsyncRouteListener_onRouteAdvanced]
90 };
91 }
92
97 if (asyncRouteManager == null) {
98 System.err.println("AsyncRouteManager not initialized");
99 return;
100 }
101
102 // Create test destination point
103 Point destinationPoint = new Point(100.0, 100.0);
104 LocationPoint destinationLocationPoint = new LocationPoint(destinationPoint, 12345, 1);
105
106 // Create route options
107 RouteOptions routeOptions = new RouteOptions(2.0, 5.0, 2.0);
108
109 // [java_AsyncRouteManager_createRouteSession]
110 // Create route session with default graph tag
111 RouteSession session = asyncRouteManager.createRouteSession(destinationLocationPoint, routeOptions);
112 System.out.println("Created route session with default graph tag");
113 // [java_AsyncRouteManager_createRouteSession]
114
115 // [java_AsyncRouteManager_createRouteSessionWithTag]
116 // Create route session with specific graph tag
117 RouteSession sessionWithTag = asyncRouteManager.createRouteSessionWithTag(destinationLocationPoint, routeOptions, "main");
118 System.out.println("Created route session with 'main' graph tag");
119 // [java_AsyncRouteManager_createRouteSessionWithTag]
120
121 // Store session for later use
122 currentSession = session;
123
124 // Add listener to session
125 if (asyncRouteListener != null) {
126 // [java_RouteSession_addRouteListener]
127 // Add route listener to session
128 session.addRouteListener(asyncRouteListener);
129 // [java_RouteSession_addRouteListener]
130 }
131 }
132
136 public void demonstrateRoutePathUsage(RoutePath routePath) {
137 if (routePath == null) {
138 System.err.println("RoutePath is null");
139 return;
140 }
141
142 // [java_RoutePath_getLength]
143 // Get route length
144 double length = routePath.getLength();
145 System.out.println("Route length: " + length + " meters");
146 // [java_RoutePath_getLength]
147
148 // [java_RoutePath_getWeight]
149 // Get total route weight
150 double weight = routePath.getWeight();
151 System.out.println("Route weight: " + weight);
152 // [java_RoutePath_getWeight]
153
154 // [java_RoutePath_getNodes]
155 // Get route nodes
156 List<RouteNode> nodes = routePath.getNodes();
157 System.out.println("Number of route nodes: " + nodes.size());
158 for (int i = 0; i < nodes.size() && i < 3; i++) {
159 demonstrateRouteNodeUsage(nodes.get(i));
160 }
161 // [java_RoutePath_getNodes]
162
163 // [java_RoutePath_head]
164 // Get route head (first 10 meters)
165 RoutePath head = routePath.head(10.0);
166 if (head != null) {
167 System.out.println("Route head length: " + head.getLength() + " meters");
168 }
169 // [java_RoutePath_head]
170
171 // [java_RoutePath_tail]
172 // Get route tail (remaining after 10 meters)
173 RoutePath tail = routePath.tail(10.0);
174 if (tail != null) {
175 System.out.println("Route tail length: " + tail.getLength() + " meters");
176 }
177 // [java_RoutePath_tail]
178 }
179
181 // [java_RouteNode_getPoint]
182 LocationPoint point = node.getPoint();
184 // [java_RouteNode_getPoint]
185
186 // [java_RouteNode_getWeight]
187 float weight = node.getWeight();
188 System.out.println("Node weight: " + weight);
189 // [java_RouteNode_getWeight]
190
191 // [java_RouteNode_getDistance]
192 float distance = node.getDistance();
193 System.out.println("Node distance: " + distance + " meters");
194 // [java_RouteNode_getDistance]
195
196 // [java_RouteNode_getEvents]
197 List<RouteEvent> events = node.getEvents();
198 for (RouteEvent event : events) {
200 }
201 // [java_RouteNode_getEvents]
202 }
203
208 if (event == null) {
209 System.err.println("RouteEvent is null");
210 return;
211 }
212
213 // [java_RouteEvent_getType]
214 // Get event type
215 RouteEventType type = event.getType();
216 System.out.println("Event type: " + type);
217 // [java_RouteEvent_getType]
218
219 // [java_RouteEvent_getTurnEvent]
220 TurnEvent turnEvent = event.getTurnEvent();
221 if (turnEvent != null) {
222 demonstrateTurnEventUsage(turnEvent);
223 }
224 // [java_RouteEvent_getTurnEvent]
225
226 // [java_RouteEvent_getTransitionEntryEvent]
227 TransitionEntryEvent entryEvent = event.getTransitionEntryEvent();
228 if (entryEvent != null) {
230 }
231 // [java_RouteEvent_getTransitionEntryEvent]
232
233 // [java_RouteEvent_getTransitionExitEvent]
234 TransitionExitEvent exitEvent = event.getTransitionExitEvent();
235 if (exitEvent != null) {
237 }
238 // [java_RouteEvent_getTransitionExitEvent]
239
240 // [java_RouteEvent_getTargetReachedEvent]
241 TargetReachedEvent reachedEvent = event.getTargetReachedEvent();
242 if (reachedEvent != null) {
244 }
245 // [java_RouteEvent_getTargetReachedEvent]
246 }
247
252 // [java_RouteEventType_values]
253 // Get all route event type values
254 RouteEventType[] types = RouteEventType.values();
255 System.out.println("Available route event types:");
256 for (RouteEventType type : types) {
257 System.out.println(" - " + type);
258 }
259 // [java_RouteEventType_values]
260 }
261
262 public void demonstrateTurnTypes() {
263 // [java_TurnType_values]
264 TurnType[] types = TurnType.values();
265 for (TurnType type : types) {
266 System.out.println(" - " + type);
267 }
268 // [java_TurnType_values]
269 }
270
272 // [java_TurnEvent_getType]
273 TurnType type = event.getType();
274 System.out.println("Turn type: " + type);
275 // [java_TurnEvent_getType]
276
277 // [java_TurnEvent_getAngle]
278 int angle = event.getAngle();
279 System.out.println("Turn angle: " + angle);
280 // [java_TurnEvent_getAngle]
281 }
282
284 // [java_TransitionEntryEvent_getFrom]
285 long from = event.getFrom();
286 System.out.println("Transition entry from: " + from);
287 // [java_TransitionEntryEvent_getFrom]
288
289 // [java_TransitionEntryEvent_getTo]
290 long to = event.getTo();
291 System.out.println("Transition entry to: " + to);
292 // [java_TransitionEntryEvent_getTo]
293 }
294
296 // [java_TransitionExitEvent_getFrom]
297 long from = event.getFrom();
298 System.out.println("Transition exit from: " + from);
299 // [java_TransitionExitEvent_getFrom]
300
301 // [java_TransitionExitEvent_getTo]
302 long to = event.getTo();
303 System.out.println("Transition exit to: " + to);
304 // [java_TransitionExitEvent_getTo]
305 }
306
308 // [java_TargetReachedEvent_getIndex]
309 long index = event.getIndex();
310 System.out.println("Reached target index: " + index);
311 // [java_TargetReachedEvent_getIndex]
312
313 // [java_TargetReachedEvent_getPoint]
314 LocationPoint point = event.getPoint();
316 // [java_TargetReachedEvent_getPoint]
317 }
318
322 public void demonstrateLocationPointUsage(LocationPoint locationPoint) {
323 if (locationPoint == null) {
324 System.err.println("LocationPoint is null");
325 return;
326 }
327
328 // [java_LocationPoint_getPoint]
329 // Get point coordinates
330 Point point = locationPoint.getPoint();
331 System.out.println("Location point: (" + point.getX() + ", " + point.getY() + ")");
332 // [java_LocationPoint_getPoint]
333
334 // [java_LocationPoint_getLocationId]
335 // Get location ID
336 int locationId = locationPoint.getLocationId();
337 System.out.println("Location ID: " + locationId);
338 // [java_LocationPoint_getLocationId]
339
340 // [java_LocationPoint_getSublocationId]
341 // Get sublocation ID
342 int sublocationId = locationPoint.getSublocationId();
343 System.out.println("Sublocation ID: " + sublocationId);
344 // [java_LocationPoint_getSublocationId]
345
346 // [java_LocationPoint_constructor]
347 // Create new location point
348 Point newPoint = new Point(25.0, 35.0);
349 LocationPoint newLocationPoint = new LocationPoint(newPoint, 12345, 2);
350 System.out.println("Created new location point: " + newLocationPoint.getPoint().getX() + ", " + newLocationPoint.getPoint().getY());
351 // [java_LocationPoint_constructor]
352 }
353
357 public void demonstratePointUsage(Point point) {
358 if (point == null) {
359 System.err.println("Point is null");
360 return;
361 }
362
363 // [java_Point_getX]
364 // Get X coordinate
365 double x = point.getX();
366 System.out.println("Point X: " + x);
367 // [java_Point_getX]
368
369 // [java_Point_getY]
370 // Get Y coordinate
371 double y = point.getY();
372 System.out.println("Point Y: " + y);
373 // [java_Point_getY]
374
375 // [java_Point_constructor]
376 // Create new point
377 Point newPoint = new Point(15.0, 25.0);
378 System.out.println("Created new point: (" + newPoint.getX() + ", " + newPoint.getY() + ")");
379 // [java_Point_constructor]
380 }
381
386 System.out.println("=== Route Options ===");
387
388 // [java_RouteOptions_constructor]
389 // Create route options with custom parameters
390 RouteOptions routeOptions = new RouteOptions(3.0, 7.0, 2.5);
391 System.out.println("Created route options with smoothRadius: " + routeOptions.getSmoothRadius() +
392 ", maxProjectionDistance: " + routeOptions.getMaxProjectionDistance() +
393 ", maxAdvance: " + routeOptions.getMaxAdvance());
394 // [java_RouteOptions_constructor]
395
396 // Demonstrate different route options configurations
397 RouteOptions[] optionsList = {
398 new RouteOptions(0.0, 5.0, 2.0), // No smoothing
399 new RouteOptions(2.0, 10.0, 1.0), // High smoothing, high projection distance
400 new RouteOptions(1.0, 3.0, 3.0), // Low smoothing, low projection distance
401 };
402
403 for (int i = 0; i < optionsList.length; i++) {
404 RouteOptions options = optionsList[i];
405 System.out.println("Options " + (i + 1) + ": smoothRadius=" + options.getSmoothRadius() +
406 ", maxProjectionDistance=" + options.getMaxProjectionDistance() +
407 ", maxAdvance=" + options.getMaxAdvance());
408 }
409 }
410
415 System.out.println("=== Advanced Async Routing Features ===");
416
417 if (asyncRouteManager == null) {
418 return;
419 }
420
421 // Create multiple destination points
422 List<LocationPoint> destinations = new ArrayList<>();
423 destinations.add(new LocationPoint(new Point(50.0, 50.0), 12345, 1));
424 destinations.add(new LocationPoint(new Point(100.0, 100.0), 12345, 1));
425 destinations.add(new LocationPoint(new Point(150.0, 150.0), 12345, 1));
426
427 // Create different route options for each destination
428 RouteOptions[] optionsList = {
429 new RouteOptions(2.0, 5.0, 2.0),
430 new RouteOptions(3.0, 7.0, 1.5),
431 new RouteOptions(1.0, 3.0, 2.5),
432 };
433
434 // Create sessions for each destination
435 List<RouteSession> sessions = new ArrayList<>();
436 for (int i = 0; i < destinations.size(); i++) {
437 RouteSession session = asyncRouteManager.createRouteSession(destinations.get(i), optionsList[i]);
438 sessions.add(session);
439 System.out.println("Created session " + (i + 1) + " for destination: " +
440 destinations.get(i).getPoint().getX() + ", " + destinations.get(i).getPoint().getY());
441
442 if (asyncRouteListener != null) {
443 session.addRouteListener(asyncRouteListener);
444 }
445
446 try {
447 Thread.sleep(1000);
448 } catch (InterruptedException e) {
449 Thread.currentThread().interrupt();
450 }
451 }
452
453 // Wait for route updates
454 try {
455 Thread.sleep(3000);
456 } catch (InterruptedException e) {
457 Thread.currentThread().interrupt();
458 }
459
460 // Cancel all sessions
461 for (int i = 0; i < sessions.size(); i++) {
462 // [java_AsyncRouteManager_cancelRouteSession]
463 // Cancel route session
464 asyncRouteManager.cancelRouteSession(sessions.get(i));
465 System.out.println("Cancelled session " + (i + 1));
466 // [java_AsyncRouteManager_cancelRouteSession]
467
468 try {
469 Thread.sleep(1000);
470 } catch (InterruptedException e) {
471 Thread.currentThread().interrupt();
472 }
473 }
474 }
475
480 System.out.println("=== RouteStatus Enum Usage ===");
481
482 // [java_RouteStatus_values]
483 // Demonstrate all RouteStatus values
484 System.out.println("Available RouteStatus values:");
485 for (RouteStatus status : RouteStatus.values()) {
486 System.out.println(" - " + status.name() + ": " + status.toString());
487 }
488
489 // Demonstrate status checking
490 RouteStatus testStatus = RouteStatus.NEW_ROUTE;
491 if (testStatus == RouteStatus.NEW_ROUTE) {
492 System.out.println("Router is ready for navigation");
493 } else if (testStatus == RouteStatus.MISSING_GRAPH) {
494 System.out.println("Router is missing the route graph");
495 } else if (testStatus == RouteStatus.MISSING_POSITION) {
496 System.out.println("Router is missing the current position");
497 } else if (testStatus == RouteStatus.MISSING_PROJECTION) {
498 System.out.println("Current position is off the route graph");
499 } else if (testStatus == RouteStatus.MISSING_ROUTE) {
500 System.out.println("Router unable to find the route to the destination point");
501 }
502 // [java_RouteStatus_values]
503 }
504
509 System.out.println("=== Route Session Management ===");
510
511 if (asyncRouteManager == null || asyncRouteListener == null) {
512 return;
513 }
514
515 // Create test destination
516 Point destinationPoint = new Point(75.0, 75.0);
517 LocationPoint destinationLocationPoint = new LocationPoint(destinationPoint, 12345, 1);
518 RouteOptions routeOptions = new RouteOptions(2.0, 5.0, 2.0);
519
520 // Create session with default tag
521 RouteSession defaultSession = asyncRouteManager.createRouteSession(destinationLocationPoint, routeOptions);
522 System.out.println("Created session with default tag");
523
524 // Create session with specific tag
525 RouteSession taggedSession = asyncRouteManager.createRouteSessionWithTag(destinationLocationPoint, routeOptions, "elevator");
526 System.out.println("Created session with 'elevator' tag");
527
528 // Add listeners to both sessions
529 defaultSession.addRouteListener(asyncRouteListener);
530 taggedSession.addRouteListener(asyncRouteListener);
531
532 // Simulate some time passing
533 new Thread(() -> {
534 try {
535 Thread.sleep(2000);
536 } catch (InterruptedException e) {
537 Thread.currentThread().interrupt();
538 }
539
540 // Remove listeners
541 // [java_RouteSession_removeRouteListener1]
542 // Remove route listener from session
543 defaultSession.removeRouteListener(asyncRouteListener);
544 taggedSession.removeRouteListener(asyncRouteListener);
545 System.out.println("Removed listeners from sessions");
546 // [java_RouteSession_removeRouteListener1]
547
548 // Cancel sessions
549 asyncRouteManager.cancelRouteSession(defaultSession);
550 asyncRouteManager.cancelRouteSession(taggedSession);
551 System.out.println("Cancelled both sessions");
552 }).start();
553 }
554
558 public void cleanup() {
559 if (currentSession != null && asyncRouteListener != null) {
560 // [java_RouteSession_removeRouteListener]
561 // Remove route listener
562 currentSession.removeRouteListener(asyncRouteListener);
563 // [java_RouteSession_removeRouteListener]
564 }
565
566 if (asyncRouteManager != null && currentSession != null) {
567 // [java_AsyncRouteManager_cancelRouteSession_1]
568 // Cancel route session
569 asyncRouteManager.cancelRouteSession(currentSession);
570 // [java_AsyncRouteManager_cancelRouteSession_1]
571 }
572 }
573
577 public void runExample() {
578 System.out.println("=== AsyncRouteManager Example ===");
579
587
588 // Wait a bit for route updates
589 try {
590 Thread.sleep(3000);
591 } catch (InterruptedException e) {
592 Thread.currentThread().interrupt();
593 }
594
595 cleanup();
596 System.out.println("=== Example completed ===");
597 }
598
602 public static void main(String[] args) {
604 example.runExample();
605 }
606}