Loading...
Searching...
No Matches
RouteManagerExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import java.util.List;
4import java.util.ArrayList;
5
10public class RouteManagerExample {
11 private NavigineSdk sdk;
12 private LocationManager locationManager;
13 private NavigationManager navigationManager;
14 private RouteManager routeManager;
15 private RouteListener routeListener;
16
18 initializeSdk();
19 setupRouteListener();
20 }
21
25 private void initializeSdk() {
26 try {
27 // [java_NavigineSdk_getInstance]
28 // Get SDK instance
30 // [java_NavigineSdk_getInstance]
31
32 // [java_NavigineSdk_setUserHash]
33 // Set user hash
34 sdk.setUserHash("USER-HASH-HERE");
35 // [java_NavigineSdk_setUserHash]
36
37 // [java_NavigineSdk_setServer]
38 // Set server URL (optional)
39 sdk.setServer("https://custom.navigine.com");
40 // [java_NavigineSdk_setServer]
41
42 // [java_NavigineSdk_getLocationManager]
43 // Get LocationManager for working with locations
44 locationManager = sdk.getLocationManager();
45 // [java_NavigineSdk_getLocationManager]
46
47 // [java_NavigineSdk_getNavigationManager]
48 // Get NavigationManager for working with navigation
49 navigationManager = sdk.getNavigationManager(locationManager);
50 // [java_NavigineSdk_getNavigationManager]
51
52 // [java_NavigineSdk_getRouteManager]
53 // Get RouteManager for working with routes
54 routeManager = sdk.getRouteManager(locationManager, navigationManager);
55 // [java_NavigineSdk_getRouteManager]
56
57 if (locationManager != null && navigationManager != null && routeManager != null) {
58 System.out.println("LocationManager, NavigationManager and RouteManager successfully initialized");
59 }
60 } catch (Exception e) {
61 System.err.println("Error initializing SDK: " + e.getMessage());
62 }
63 }
64
68 private void setupRouteListener() {
69 routeListener = new RouteListener() {
70 @Override
71 // [java_RouteListener_onPathsUpdated]
72 public void onPathsUpdated(List<RoutePath> paths) {
73 System.out.println("Routes updated successfully");
75 }
76 // [java_RouteListener_onPathsUpdated]
77 };
78 }
79
84 if (routeManager == null) {
85 System.err.println("RouteManager not initialized");
86 return;
87 }
88
89 if (routeListener == null) return;
90
91 // [java_RouteManager_addRouteListener]
92 // Add route listener
93 routeManager.addRouteListener(routeListener);
94 // [java_RouteManager_addRouteListener]
95
96 // [java_RouteManager_setGraphTag]
97 // Set graph tag
98 routeManager.setGraphTag("main");
99 // [java_RouteManager_setGraphTag]
100
101 // [java_RouteManager_getGraphTag]
102 // Get current graph tag
103 String currentGraphTag = routeManager.getGraphTag();
104 System.out.println("Current graph tag: " + currentGraphTag);
105 // [java_RouteManager_getGraphTag]
106
107 // [java_RouteManager_getGraphTags]
108 // Get all graph tags
109 List<String> graphTags = routeManager.getGraphTags();
110 System.out.println("Available graph tags: " + graphTags);
111 // [java_RouteManager_getGraphTags]
112
113 // Create test points
114 Point startPoint = new Point(10.0, 20.0);
115 Point endPoint = new Point(50.0, 60.0);
116 LocationPoint startLocationPoint = new LocationPoint(startPoint, 12345, 1);
117 LocationPoint endLocationPoint = new LocationPoint(endPoint, 12345, 1);
118
119 // [java_RouteManager_makeRoute]
120 // Make route from point to point
121 RoutePath routePath = routeManager.makeRoute(startLocationPoint, endLocationPoint);
122 System.out.println("Route created with length: " + routePath.getLength() + " meters");
123 // [java_RouteManager_makeRoute]
124
125 // [java_RouteManager_makeRoutes]
126 // Make routes to multiple destinations
127 List<LocationPoint> destinations = new ArrayList<>();
128 destinations.add(new LocationPoint(new Point(30.0, 40.0), 12345, 1));
129 destinations.add(new LocationPoint(new Point(70.0, 80.0), 12345, 1));
130 List<RoutePath> routePaths = routeManager.makeRoutes(startLocationPoint, destinations);
131 System.out.println("Created " + routePaths.size() + " routes");
132 // [java_RouteManager_makeRoutes]
133
134 // [java_RouteManager_setTarget]
135 // Set target point
136 routeManager.setTarget(endLocationPoint);
137 // [java_RouteManager_setTarget]
138
139 // [java_RouteManager_addTarget]
140 // Add additional target point
141 LocationPoint additionalTarget = new LocationPoint(new Point(90.0, 100.0), 12345, 1);
142 routeManager.addTarget(additionalTarget);
143 // [java_RouteManager_addTarget]
144
145 // [java_RouteManager_cancelTarget]
146 // Cancel current target
147 routeManager.cancelTarget();
148 // [java_RouteManager_cancelTarget]
149
150 // [java_RouteManager_clearTargets]
151 // Clear all targets
152 routeManager.clearTargets();
153 // [java_RouteManager_clearTargets]
154 }
155
159 public void demonstrateRoutePathUsage(List<RoutePath> paths) {
160 for (int i = 0; i < paths.size(); i++) {
161 RoutePath path = paths.get(i);
162 System.out.println("=== Route Path " + (i + 1) + " ===");
163
164 // [java_RoutePath_getLength]
165 // Get route length
166 double length = path.getLength();
167 System.out.println("Route length: " + length + " meters");
168 // [java_RoutePath_getLength]
169
170 // [java_RoutePath_getWeight]
171 // Get total route weight
172 double weight = path.getWeight();
173 System.out.println("Route weight: " + weight);
174 // [java_RoutePath_getWeight]
175
176 // [java_RoutePath_getNodes]
177 // Get route nodes
178 List<RouteNode> nodes = path.getNodes();
179 System.out.println("Route has " + nodes.size() + " nodes");
180 for (RouteNode node : nodes) {
182 }
183 // [java_RoutePath_getNodes]
184
185 // [java_RoutePath_head]
186 // Get head of route (first 10 meters)
187 RoutePath headPath = path.head(10.0);
188 if (headPath != null) {
189 System.out.println("Head path length: " + headPath.getLength() + " meters");
190 }
191 // [java_RoutePath_head]
192
193 // [java_RoutePath_tail]
194 // Get tail of route (remaining after 10 meters)
195 RoutePath tailPath = path.tail(10.0);
196 if (tailPath != null) {
197 System.out.println("Tail path length: " + tailPath.getLength() + " meters");
198 }
199 // [java_RoutePath_tail]
200 }
201 }
202
207 // [java_RouteNode_getPoint]
208 LocationPoint point = node.getPoint();
209 if (point != null) {
211 }
212 // [java_RouteNode_getPoint]
213
214 // [java_RouteNode_getWeight]
215 float weight = node.getWeight();
216 System.out.println("Node weight: " + weight);
217 // [java_RouteNode_getWeight]
218
219 // [java_RouteNode_getDistance]
220 float distance = node.getDistance();
221 System.out.println("Node distance: " + distance + " meters");
222 // [java_RouteNode_getDistance]
223
224 // [java_RouteNode_getEvents]
225 List<RouteEvent> events = node.getEvents();
226 System.out.println("Node has " + events.size() + " events");
227 for (RouteEvent event : events) {
229 }
230 // [java_RouteNode_getEvents]
231 }
232
237 // [java_RouteEvent_getType]
238 // Get event type
239 RouteEventType type = event.getType();
240 System.out.println("Event type: " + type);
241 // [java_RouteEvent_getType]
242
243 // [java_RouteEvent_getTurnEvent]
244 TurnEvent turnEvent = event.getTurnEvent();
245 if (turnEvent != null) {
246 demonstrateTurnEventUsage(turnEvent);
247 }
248 // [java_RouteEvent_getTurnEvent]
249
250 // [java_RouteEvent_getTransitionEntryEvent]
251 TransitionEntryEvent entryEvent = event.getTransitionEntryEvent();
252 if (entryEvent != null) {
254 }
255 // [java_RouteEvent_getTransitionEntryEvent]
256
257 // [java_RouteEvent_getTransitionExitEvent]
258 TransitionExitEvent exitEvent = event.getTransitionExitEvent();
259 if (exitEvent != null) {
261 }
262 // [java_RouteEvent_getTransitionExitEvent]
263
264 // [java_RouteEvent_getTargetReachedEvent]
265 TargetReachedEvent reachedEvent = event.getTargetReachedEvent();
266 if (reachedEvent != null) {
268 }
269 // [java_RouteEvent_getTargetReachedEvent]
270 }
271
276 // [java_RouteEventType_values]
277 // Get all route event type values
278 RouteEventType[] types = RouteEventType.values();
279 System.out.println("Available route event types:");
280 for (RouteEventType type : types) {
281 System.out.println(" - " + type);
282 }
283 // [java_RouteEventType_values]
284 }
285
289 public void demonstrateTurnTypes() {
290 // [java_TurnType_values]
291 TurnType[] turnTypes = TurnType.values();
292 System.out.println("Available turn types:");
293 for (TurnType turnType : turnTypes) {
294 System.out.println(" - " + turnType);
295 }
296 // [java_TurnType_values]
297 }
298
299 public void demonstrateTurnEventUsage(TurnEvent turnEvent) {
300 // [java_TurnEvent_getType]
301 TurnType turnType = turnEvent.getType();
302 System.out.println("Turn type: " + turnType);
303 // [java_TurnEvent_getType]
304
305 // [java_TurnEvent_getAngle]
306 int angle = turnEvent.getAngle();
307 System.out.println("Turn angle: " + angle);
308 // [java_TurnEvent_getAngle]
309 }
310
312 // [java_TransitionEntryEvent_getFrom]
313 long from = event.getFrom();
314 System.out.println("Transition entry from: " + from);
315 // [java_TransitionEntryEvent_getFrom]
316
317 // [java_TransitionEntryEvent_getTo]
318 long to = event.getTo();
319 System.out.println("Transition entry to: " + to);
320 // [java_TransitionEntryEvent_getTo]
321 }
322
324 // [java_TransitionExitEvent_getFrom]
325 long from = event.getFrom();
326 System.out.println("Transition exit from: " + from);
327 // [java_TransitionExitEvent_getFrom]
328
329 // [java_TransitionExitEvent_getTo]
330 long to = event.getTo();
331 System.out.println("Transition exit to: " + to);
332 // [java_TransitionExitEvent_getTo]
333 }
334
336 // [java_TargetReachedEvent_getIndex]
337 long index = event.getIndex();
338 System.out.println("Reached target index: " + index);
339 // [java_TargetReachedEvent_getIndex]
340
341 // [java_TargetReachedEvent_getPoint]
342 LocationPoint point = event.getPoint();
343 if (point != null) {
345 }
346 // [java_TargetReachedEvent_getPoint]
347 }
348
352 public void demonstrateLocationPointUsage(LocationPoint locationPoint) {
353 // [java_LocationPoint_getPoint]
354 // Get point coordinates
355 Point point = locationPoint.getPoint();
356 if (point != null) {
358 }
359 // [java_LocationPoint_getPoint]
360
361 // [java_LocationPoint_getLocationId]
362 // Get location ID
363 int locationId = locationPoint.getLocationId();
364 System.out.println("Location ID: " + locationId);
365 // [java_LocationPoint_getLocationId]
366
367 // [java_LocationPoint_getSublocationId]
368 // Get sublocation ID
369 int sublocationId = locationPoint.getSublocationId();
370 System.out.println("Sublocation ID: " + sublocationId);
371 // [java_LocationPoint_getSublocationId]
372
373 // [java_LocationPoint_constructor]
374 // Create new LocationPoint
375 Point newPoint = new Point(15.0, 25.0);
376 LocationPoint newLocationPoint = new LocationPoint(newPoint, locationId, sublocationId);
377 System.out.println("Created new LocationPoint: " + newLocationPoint);
378 // [java_LocationPoint_constructor]
379 }
380
384 public void demonstratePointUsage(Point point) {
385 // [java_Point_getX]
386 // Get X coordinate
387 double x = point.getX();
388 System.out.println("Point X: " + x);
389 // [java_Point_getX]
390
391 // [java_Point_getY]
392 // Get Y coordinate
393 double y = point.getY();
394 System.out.println("Point Y: " + y);
395 // [java_Point_getY]
396
397 // [java_Point_constructor]
398 // Create new Point
399 Point newPoint = new Point(x, y);
400 System.out.println("Created new Point: " + newPoint);
401 // [java_Point_constructor]
402 }
403
408 System.out.println("=== Advanced Routing Features ===");
409
410 if (routeManager == null) {
411 return;
412 }
413
414 // Create multiple routes with different graph tags
415 String[] graphTags = {"main", "elevator", "stairs"};
416
417 for (String tag : graphTags) {
418 // [java_RouteManager_setGraphTag_2]
419 // Set different graph tag for routing
420 routeManager.setGraphTag(tag);
421 // [java_RouteManager_setGraphTag_2]
422
423 System.out.println("Routing with graph tag: " + tag);
424
425 // Create test route
426 Point startPoint = new Point(0.0, 0.0);
427 Point endPoint = new Point(100.0, 100.0);
428 LocationPoint startLocationPoint = new LocationPoint(startPoint, 12345, 1);
429 LocationPoint endLocationPoint = new LocationPoint(endPoint, 12345, 1);
430
431 // [java_RouteManager_makeRoute_2]
432 // Make route with specific graph tag
433 RoutePath routePath = routeManager.makeRoute(startLocationPoint, endLocationPoint);
434 // [java_RouteManager_makeRoute_2]
435
436 System.out.println("Route with tag '" + tag + "' has length: " + routePath.getLength() + " meters");
437 }
438 }
439
444 System.out.println("=== Route Planning Simulation ===");
445
446 if (routeManager == null) {
447 return;
448 }
449
450 // Create multiple target points
451 List<LocationPoint> targets = new ArrayList<>();
452 targets.add(new LocationPoint(new Point(50.0, 50.0), 12345, 1));
453 targets.add(new LocationPoint(new Point(100.0, 100.0), 12345, 1));
454 targets.add(new LocationPoint(new Point(150.0, 150.0), 12345, 1));
455
456 // [java_RouteManager_setTarget_2]
457 // Set first target
458 routeManager.setTarget(targets.get(0));
459 // [java_RouteManager_setTarget_2]
460
461 System.out.println("Set target 1: " + targets.get(0));
462
463 try {
464 Thread.sleep(1000);
465 } catch (InterruptedException e) {
466 Thread.currentThread().interrupt();
467 }
468
469 // [java_RouteManager_addTarget_2]
470 // Add additional targets
471 for (int i = 1; i < targets.size(); i++) {
472 routeManager.addTarget(targets.get(i));
473 System.out.println("Added target " + (i + 1) + ": " + targets.get(i));
474 try {
475 Thread.sleep(1000);
476 } catch (InterruptedException e) {
477 Thread.currentThread().interrupt();
478 }
479 }
480 // [java_RouteManager_addTarget_2]
481
482 // [java_RouteManager_cancelTarget_2]
483 // Cancel current target
484 routeManager.cancelTarget();
485 System.out.println("Cancelled current target");
486 // [java_RouteManager_cancelTarget_2]
487
488 try {
489 Thread.sleep(1000);
490 } catch (InterruptedException e) {
491 Thread.currentThread().interrupt();
492 }
493
494 // [java_RouteManager_clearTargets_2]
495 // Clear all targets
496 routeManager.clearTargets();
497 System.out.println("Cleared all targets");
498 // [java_RouteManager_clearTargets_2]
499 }
500
504 public void cleanup() {
505 if (routeManager != null && routeListener != null) {
506 // [java_RouteManager_removeRouteListener]
507 // Remove route listener
508 routeManager.removeRouteListener(routeListener);
509 // [java_RouteManager_removeRouteListener]
510 }
511 }
512
516 public void runExample() {
517 System.out.println("=== RouteManager Example ===");
518
524
525 // Wait a bit for route updates
526 try {
527 Thread.sleep(3000);
528 } catch (InterruptedException e) {
529 Thread.currentThread().interrupt();
530 }
531
532 cleanup();
533 System.out.println("=== Example completed ===");
534 }
535
539 public static void main(String[] args) {
541 example.runExample();
542 }
543}