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
30 sdk = NavigineSdk.getInstance();
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_getPoints]
149 // Get route points
150 List<LocationPoint> points = routePath.getPoints();
151 System.out.println("Number of route points: " + points.size());
152 for (int i = 0; i < points.size() && i < 3; i++) {
153 System.out.println("Point " + (i + 1) + ": " + points.get(i));
154 }
155 // [java_RoutePath_getPoints]
156
157 // [java_RoutePath_getEvents]
158 // Get route events
159 List<RouteEvent> events = routePath.getEvents();
160 System.out.println("Number of route events: " + events.size());
161 for (int i = 0; i < events.size() && i < 3; i++) {
162 demonstrateRouteEventUsage(events.get(i));
163 }
164 // [java_RoutePath_getEvents]
165
166 // [java_RoutePath_head]
167 // Get route head (first 10 meters)
168 RoutePath head = routePath.head(10.0);
169 if (head != null) {
170 System.out.println("Route head length: " + head.getLength() + " meters");
171 }
172 // [java_RoutePath_head]
173
174 // [java_RoutePath_tail]
175 // Get route tail (remaining after 10 meters)
176 RoutePath tail = routePath.tail(10.0);
177 if (tail != null) {
178 System.out.println("Route tail length: " + tail.getLength() + " meters");
179 }
180 // [java_RoutePath_tail]
181 }
182
186 public void demonstrateRouteEventUsage(RouteEvent event) {
187 if (event == null) {
188 System.err.println("RouteEvent is null");
189 return;
190 }
191
192 // [java_RouteEvent_getType]
193 // Get event type
194 RouteEventType type = event.getType();
195 System.out.println("Event type: " + type);
196 // [java_RouteEvent_getType]
197
198 // [java_RouteEvent_getValue]
199 // Get event value
200 int value = event.getValue();
201 System.out.println("Event value: " + value);
202 // [java_RouteEvent_getValue]
203
204 // [java_RouteEvent_getDistance]
205 // Get event distance
206 double distance = event.getDistance();
207 System.out.println("Event distance: " + distance + " meters");
208 // [java_RouteEvent_getDistance]
209
210 // [java_RouteEvent_constructor]
211 // Create new route event
212 RouteEvent newEvent = new RouteEvent(RouteEventType.TURN_LEFT, 90, 50.0);
213 System.out.println("Created new route event: " + newEvent.getType() + " at " + newEvent.getDistance() + " meters");
214 // [java_RouteEvent_constructor]
215 }
216
221 // [java_RouteEventType_values]
222 // Get all route event type values
223 RouteEventType[] types = RouteEventType.values();
224 System.out.println("Available route event types:");
225 for (RouteEventType type : types) {
226 System.out.println(" - " + type);
227 }
228 // [java_RouteEventType_values]
229 }
230
234 public void demonstrateLocationPointUsage(LocationPoint locationPoint) {
235 if (locationPoint == null) {
236 System.err.println("LocationPoint is null");
237 return;
238 }
239
240 // [java_LocationPoint_getPoint]
241 // Get point coordinates
242 Point point = locationPoint.getPoint();
243 System.out.println("Location point: (" + point.getX() + ", " + point.getY() + ")");
244 // [java_LocationPoint_getPoint]
245
246 // [java_LocationPoint_getLocationId]
247 // Get location ID
248 int locationId = locationPoint.getLocationId();
249 System.out.println("Location ID: " + locationId);
250 // [java_LocationPoint_getLocationId]
251
252 // [java_LocationPoint_getSublocationId]
253 // Get sublocation ID
254 int sublocationId = locationPoint.getSublocationId();
255 System.out.println("Sublocation ID: " + sublocationId);
256 // [java_LocationPoint_getSublocationId]
257
258 // [java_LocationPoint_constructor]
259 // Create new location point
260 Point newPoint = new Point(25.0, 35.0);
261 LocationPoint newLocationPoint = new LocationPoint(newPoint, 12345, 2);
262 System.out.println("Created new location point: " + newLocationPoint.getPoint().getX() + ", " + newLocationPoint.getPoint().getY());
263 // [java_LocationPoint_constructor]
264 }
265
269 public void demonstratePointUsage(Point point) {
270 if (point == null) {
271 System.err.println("Point is null");
272 return;
273 }
274
275 // [java_Point_getX]
276 // Get X coordinate
277 double x = point.getX();
278 System.out.println("Point X: " + x);
279 // [java_Point_getX]
280
281 // [java_Point_getY]
282 // Get Y coordinate
283 double y = point.getY();
284 System.out.println("Point Y: " + y);
285 // [java_Point_getY]
286
287 // [java_Point_constructor]
288 // Create new point
289 Point newPoint = new Point(15.0, 25.0);
290 System.out.println("Created new point: (" + newPoint.getX() + ", " + newPoint.getY() + ")");
291 // [java_Point_constructor]
292 }
293
298 System.out.println("=== Route Options ===");
299
300 // [java_RouteOptions_constructor]
301 // Create route options with custom parameters
302 RouteOptions routeOptions = new RouteOptions(3.0, 7.0, 2.5);
303 System.out.println("Created route options with smoothRadius: " + routeOptions.getSmoothRadius() +
304 ", maxProjectionDistance: " + routeOptions.getMaxProjectionDistance() +
305 ", maxAdvance: " + routeOptions.getMaxAdvance());
306 // [java_RouteOptions_constructor]
307
308 // Demonstrate different route options configurations
309 RouteOptions[] optionsList = {
310 new RouteOptions(0.0, 5.0, 2.0), // No smoothing
311 new RouteOptions(2.0, 10.0, 1.0), // High smoothing, high projection distance
312 new RouteOptions(1.0, 3.0, 3.0), // Low smoothing, low projection distance
313 };
314
315 for (int i = 0; i < optionsList.length; i++) {
316 RouteOptions options = optionsList[i];
317 System.out.println("Options " + (i + 1) + ": smoothRadius=" + options.getSmoothRadius() +
318 ", maxProjectionDistance=" + options.getMaxProjectionDistance() +
319 ", maxAdvance=" + options.getMaxAdvance());
320 }
321 }
322
327 System.out.println("=== Advanced Async Routing Features ===");
328
329 if (asyncRouteManager == null) {
330 return;
331 }
332
333 // Create multiple destination points
334 List<LocationPoint> destinations = new ArrayList<>();
335 destinations.add(new LocationPoint(new Point(50.0, 50.0), 12345, 1));
336 destinations.add(new LocationPoint(new Point(100.0, 100.0), 12345, 1));
337 destinations.add(new LocationPoint(new Point(150.0, 150.0), 12345, 1));
338
339 // Create different route options for each destination
340 RouteOptions[] optionsList = {
341 new RouteOptions(2.0, 5.0, 2.0),
342 new RouteOptions(3.0, 7.0, 1.5),
343 new RouteOptions(1.0, 3.0, 2.5),
344 };
345
346 // Create sessions for each destination
347 List<RouteSession> sessions = new ArrayList<>();
348 for (int i = 0; i < destinations.size(); i++) {
349 RouteSession session = asyncRouteManager.createRouteSession(destinations.get(i), optionsList[i]);
350 sessions.add(session);
351 System.out.println("Created session " + (i + 1) + " for destination: " +
352 destinations.get(i).getPoint().getX() + ", " + destinations.get(i).getPoint().getY());
353
354 if (asyncRouteListener != null) {
355 session.addRouteListener(asyncRouteListener);
356 }
357
358 try {
359 Thread.sleep(1000);
360 } catch (InterruptedException e) {
361 Thread.currentThread().interrupt();
362 }
363 }
364
365 // Wait for route updates
366 try {
367 Thread.sleep(3000);
368 } catch (InterruptedException e) {
369 Thread.currentThread().interrupt();
370 }
371
372 // Cancel all sessions
373 for (int i = 0; i < sessions.size(); i++) {
374 // [java_AsyncRouteManager_cancelRouteSession]
375 // Cancel route session
376 asyncRouteManager.cancelRouteSession(sessions.get(i));
377 System.out.println("Cancelled session " + (i + 1));
378 // [java_AsyncRouteManager_cancelRouteSession]
379
380 try {
381 Thread.sleep(1000);
382 } catch (InterruptedException e) {
383 Thread.currentThread().interrupt();
384 }
385 }
386 }
387
392 System.out.println("=== RouteStatus Enum Usage ===");
393
394 // [java_RouteStatus_values]
395 // Demonstrate all RouteStatus values
396 System.out.println("Available RouteStatus values:");
397 for (RouteStatus status : RouteStatus.values()) {
398 System.out.println(" - " + status.name() + ": " + status.toString());
399 }
400
401 // Demonstrate status checking
402 RouteStatus testStatus = RouteStatus.NEW_ROUTE;
403 if (testStatus == RouteStatus.NEW_ROUTE) {
404 System.out.println("Router is ready for navigation");
405 } else if (testStatus == RouteStatus.MISSING_GRAPH) {
406 System.out.println("Router is missing the route graph");
407 } else if (testStatus == RouteStatus.MISSING_POSITION) {
408 System.out.println("Router is missing the current position");
409 } else if (testStatus == RouteStatus.MISSING_PROJECTION) {
410 System.out.println("Current position is off the route graph");
411 } else if (testStatus == RouteStatus.MISSING_ROUTE) {
412 System.out.println("Router unable to find the route to the destination point");
413 }
414 // [java_RouteStatus_values]
415 }
416
421 System.out.println("=== Route Session Management ===");
422
423 if (asyncRouteManager == null || asyncRouteListener == null) {
424 return;
425 }
426
427 // Create test destination
428 Point destinationPoint = new Point(75.0, 75.0);
429 LocationPoint destinationLocationPoint = new LocationPoint(destinationPoint, 12345, 1);
430 RouteOptions routeOptions = new RouteOptions(2.0, 5.0, 2.0);
431
432 // Create session with default tag
433 RouteSession defaultSession = asyncRouteManager.createRouteSession(destinationLocationPoint, routeOptions);
434 System.out.println("Created session with default tag");
435
436 // Create session with specific tag
437 RouteSession taggedSession = asyncRouteManager.createRouteSessionWithTag(destinationLocationPoint, routeOptions, "elevator");
438 System.out.println("Created session with 'elevator' tag");
439
440 // Add listeners to both sessions
441 defaultSession.addRouteListener(asyncRouteListener);
442 taggedSession.addRouteListener(asyncRouteListener);
443
444 // Simulate some time passing
445 new Thread(() -> {
446 try {
447 Thread.sleep(2000);
448 } catch (InterruptedException e) {
449 Thread.currentThread().interrupt();
450 }
451
452 // Remove listeners
453 // [java_RouteSession_removeRouteListener]
454 // Remove route listener from session
455 defaultSession.removeRouteListener(asyncRouteListener);
456 taggedSession.removeRouteListener(asyncRouteListener);
457 System.out.println("Removed listeners from sessions");
458 // [java_RouteSession_removeRouteListener]
459
460 // Cancel sessions
461 asyncRouteManager.cancelRouteSession(defaultSession);
462 asyncRouteManager.cancelRouteSession(taggedSession);
463 System.out.println("Cancelled both sessions");
464 }).start();
465 }
466
470 public void cleanup() {
471 if (currentSession != null && asyncRouteListener != null) {
472 // [java_RouteSession_removeRouteListener]
473 // Remove route listener
474 currentSession.removeRouteListener(asyncRouteListener);
475 // [java_RouteSession_removeRouteListener]
476 }
477
478 if (asyncRouteManager != null && currentSession != null) {
479 // [java_AsyncRouteManager_cancelRouteSession_1]
480 // Cancel route session
481 asyncRouteManager.cancelRouteSession(currentSession);
482 // [java_AsyncRouteManager_cancelRouteSession_1]
483 }
484 }
485
489 public void runExample() {
490 System.out.println("=== AsyncRouteManager Example ===");
491
498
499 // Wait a bit for route updates
500 try {
501 Thread.sleep(3000);
502 } catch (InterruptedException e) {
503 Thread.currentThread().interrupt();
504 }
505
506 cleanup();
507 System.out.println("=== Example completed ===");
508 }
509
513 public static void main(String[] args) {
515 example.runExample();
516 }
517}