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
29 sdk = NavigineSdk.getInstance();
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_getPoints]
171 // Get route points
172 List<LocationPoint> points = path.getPoints();
173 System.out.println("Route has " + points.size() + " points");
174 for (int j = 0; j < points.size(); j++) {
175 demonstrateLocationPointUsage(points.get(j));
176 }
177 // [java_RoutePath_getPoints]
178
179 // [java_RoutePath_getEvents]
180 // Get route events
181 List<RouteEvent> events = path.getEvents();
182 System.out.println("Route has " + events.size() + " events");
183 for (int j = 0; j < events.size(); j++) {
184 demonstrateRouteEventUsage(events.get(j));
185 }
186 // [java_RoutePath_getEvents]
187
188 // [java_RoutePath_head]
189 // Get head of route (first 10 meters)
190 RoutePath headPath = path.head(10.0);
191 if (headPath != null) {
192 System.out.println("Head path length: " + headPath.getLength() + " meters");
193 }
194 // [java_RoutePath_head]
195
196 // [java_RoutePath_tail]
197 // Get tail of route (remaining after 10 meters)
198 RoutePath tailPath = path.tail(10.0);
199 if (tailPath != null) {
200 System.out.println("Tail path length: " + tailPath.getLength() + " meters");
201 }
202 // [java_RoutePath_tail]
203 }
204 }
205
209 public void demonstrateRouteEventUsage(RouteEvent event) {
210 // [java_RouteEvent_getType]
211 // Get event type
212 RouteEventType type = event.getType();
213 System.out.println("Event type: " + type);
214 // [java_RouteEvent_getType]
215
216 // [java_RouteEvent_getValue]
217 // Get event value (angle for turns, sublocation ID for transitions)
218 int value = event.getValue();
219 System.out.println("Event value: " + value);
220 // [java_RouteEvent_getValue]
221
222 // [java_RouteEvent_getDistance]
223 // Get distance from route start
224 double distance = event.getDistance();
225 System.out.println("Event distance: " + distance + " meters");
226 // [java_RouteEvent_getDistance]
227
228 // [java_RouteEvent_constructor]
229 // Create new RouteEvent
230 RouteEvent newEvent = new RouteEvent(type, value, distance);
231 System.out.println("Created new RouteEvent: " + newEvent);
232 // [java_RouteEvent_constructor]
233 }
234
239 // [java_RouteEventType_values]
240 // Get all route event type values
241 RouteEventType[] types = RouteEventType.values();
242 System.out.println("Available route event types:");
243 for (RouteEventType type : types) {
244 System.out.println(" - " + type);
245 }
246 // [java_RouteEventType_values]
247 }
248
252 public void demonstrateLocationPointUsage(LocationPoint locationPoint) {
253 // [java_LocationPoint_getPoint]
254 // Get point coordinates
255 Point point = locationPoint.getPoint();
256 if (point != null) {
258 }
259 // [java_LocationPoint_getPoint]
260
261 // [java_LocationPoint_getLocationId]
262 // Get location ID
263 int locationId = locationPoint.getLocationId();
264 System.out.println("Location ID: " + locationId);
265 // [java_LocationPoint_getLocationId]
266
267 // [java_LocationPoint_getSublocationId]
268 // Get sublocation ID
269 int sublocationId = locationPoint.getSublocationId();
270 System.out.println("Sublocation ID: " + sublocationId);
271 // [java_LocationPoint_getSublocationId]
272
273 // [java_LocationPoint_constructor]
274 // Create new LocationPoint
275 Point newPoint = new Point(15.0, 25.0);
276 LocationPoint newLocationPoint = new LocationPoint(newPoint, locationId, sublocationId);
277 System.out.println("Created new LocationPoint: " + newLocationPoint);
278 // [java_LocationPoint_constructor]
279 }
280
284 public void demonstratePointUsage(Point point) {
285 // [java_Point_getX]
286 // Get X coordinate
287 double x = point.getX();
288 System.out.println("Point X: " + x);
289 // [java_Point_getX]
290
291 // [java_Point_getY]
292 // Get Y coordinate
293 double y = point.getY();
294 System.out.println("Point Y: " + y);
295 // [java_Point_getY]
296
297 // [java_Point_constructor]
298 // Create new Point
299 Point newPoint = new Point(x, y);
300 System.out.println("Created new Point: " + newPoint);
301 // [java_Point_constructor]
302 }
303
308 System.out.println("=== Advanced Routing Features ===");
309
310 if (routeManager == null) {
311 return;
312 }
313
314 // Create multiple routes with different graph tags
315 String[] graphTags = {"main", "elevator", "stairs"};
316
317 for (String tag : graphTags) {
318 // [java_RouteManager_setGraphTag_2]
319 // Set different graph tag for routing
320 routeManager.setGraphTag(tag);
321 // [java_RouteManager_setGraphTag_2]
322
323 System.out.println("Routing with graph tag: " + tag);
324
325 // Create test route
326 Point startPoint = new Point(0.0, 0.0);
327 Point endPoint = new Point(100.0, 100.0);
328 LocationPoint startLocationPoint = new LocationPoint(startPoint, 12345, 1);
329 LocationPoint endLocationPoint = new LocationPoint(endPoint, 12345, 1);
330
331 // [java_RouteManager_makeRoute_2]
332 // Make route with specific graph tag
333 RoutePath routePath = routeManager.makeRoute(startLocationPoint, endLocationPoint);
334 // [java_RouteManager_makeRoute_2]
335
336 System.out.println("Route with tag '" + tag + "' has length: " + routePath.getLength() + " meters");
337 }
338 }
339
344 System.out.println("=== Route Planning Simulation ===");
345
346 if (routeManager == null) {
347 return;
348 }
349
350 // Create multiple target points
351 List<LocationPoint> targets = new ArrayList<>();
352 targets.add(new LocationPoint(new Point(50.0, 50.0), 12345, 1));
353 targets.add(new LocationPoint(new Point(100.0, 100.0), 12345, 1));
354 targets.add(new LocationPoint(new Point(150.0, 150.0), 12345, 1));
355
356 // [java_RouteManager_setTarget_2]
357 // Set first target
358 routeManager.setTarget(targets.get(0));
359 // [java_RouteManager_setTarget_2]
360
361 System.out.println("Set target 1: " + targets.get(0));
362
363 try {
364 Thread.sleep(1000);
365 } catch (InterruptedException e) {
366 Thread.currentThread().interrupt();
367 }
368
369 // [java_RouteManager_addTarget_2]
370 // Add additional targets
371 for (int i = 1; i < targets.size(); i++) {
372 routeManager.addTarget(targets.get(i));
373 System.out.println("Added target " + (i + 1) + ": " + targets.get(i));
374 try {
375 Thread.sleep(1000);
376 } catch (InterruptedException e) {
377 Thread.currentThread().interrupt();
378 }
379 }
380 // [java_RouteManager_addTarget_2]
381
382 // [java_RouteManager_cancelTarget_2]
383 // Cancel current target
384 routeManager.cancelTarget();
385 System.out.println("Cancelled current target");
386 // [java_RouteManager_cancelTarget_2]
387
388 try {
389 Thread.sleep(1000);
390 } catch (InterruptedException e) {
391 Thread.currentThread().interrupt();
392 }
393
394 // [java_RouteManager_clearTargets_2]
395 // Clear all targets
396 routeManager.clearTargets();
397 System.out.println("Cleared all targets");
398 // [java_RouteManager_clearTargets_2]
399 }
400
404 public void cleanup() {
405 if (routeManager != null && routeListener != null) {
406 // [java_RouteManager_removeRouteListener]
407 // Remove route listener
408 routeManager.removeRouteListener(routeListener);
409 // [java_RouteManager_removeRouteListener]
410 }
411 }
412
416 public void runExample() {
417 System.out.println("=== RouteManager Example ===");
418
423
424 // Wait a bit for route updates
425 try {
426 Thread.sleep(3000);
427 } catch (InterruptedException e) {
428 Thread.currentThread().interrupt();
429 }
430
431 cleanup();
432 System.out.println("=== Example completed ===");
433 }
434
438 public static void main(String[] args) {
440 example.runExample();
441 }
442}