Loading...
Searching...
No Matches
RouteManagerExample.kt
Go to the documentation of this file.
1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3
4/**
5 * RouteManager usage example for Kotlin
6 * Demonstrates working with route planning, path calculation, and navigation
7 */
8class RouteManagerExample {
9 private var sdk: NavigineSdk? = null
10 private var locationManager: LocationManager? = null
11 private var navigationManager: NavigationManager? = null
12 private var routeManager: RouteManager? = null
13 private var routeListener: RouteListener? = null
14
15 init {
16 initializeSdk()
17 setupRouteListener()
18 }
19
20 /**
21 * Initialize SDK and get managers
22 */
23 private fun initializeSdk() {
24 try {
25 // [kotlin_NavigineSdk_getInstance]
26 // Get SDK instance
27 sdk = NavigineSdk.getInstance()
28 // [kotlin_NavigineSdk_getInstance]
29
30 // [kotlin_NavigineSdk_setUserHash]
31 // Set user hash
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [kotlin_NavigineSdk_setUserHash]
34
35 // [kotlin_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [kotlin_NavigineSdk_setServer]
39
40 // [kotlin_NavigineSdk_getLocationManager]
41 // Get LocationManager for working with locations
42 locationManager = sdk?.getLocationManager()
43 // [kotlin_NavigineSdk_getLocationManager]
44
45 // [kotlin_NavigineSdk_getNavigationManager]
46 // Get NavigationManager for working with navigation
47 navigationManager = sdk?.getNavigationManager(locationManager)
48 // [kotlin_NavigineSdk_getNavigationManager]
49
50 // [kotlin_NavigineSdk_getRouteManager]
51 // Get RouteManager for working with routes
52 routeManager = sdk?.getRouteManager(locationManager, navigationManager)
53 // [kotlin_NavigineSdk_getRouteManager]
54
55 if (locationManager != null && navigationManager != null && routeManager != null) {
56 println("LocationManager, NavigationManager and RouteManager successfully initialized")
57 }
58 } catch (e: Exception) {
59 System.err.println("Error initializing SDK: ${e.message}")
60 }
61 }
62
63 /**
64 * Setup route listener
65 */
66 private fun setupRouteListener() {
67 routeListener = object : RouteListener() {
68 // [kotlin_RouteListener_onPathsUpdated]
69 override fun onPathsUpdated(paths: List<RoutePath>) {
70 println("Routes updated successfully")
71 demonstrateRoutePathUsage(paths)
72 }
73 // [kotlin_RouteListener_onPathsUpdated]
74 }
75 }
76
77 /**
78 * Demonstrate RouteManager methods
79 */
80 fun demonstrateRouteManagerMethods() {
81 val manager = routeManager ?: run {
82 System.err.println("RouteManager not initialized")
83 return
84 }
85
86 val listener = routeListener ?: return
87
88 // [kotlin_RouteManager_addRouteListener]
89 // Add route listener
90 manager.addRouteListener(listener)
91 // [kotlin_RouteManager_addRouteListener]
92
93 // [kotlin_RouteManager_setGraphTag]
94 // Set graph tag
95 manager.setGraphTag("main")
96 // [kotlin_RouteManager_setGraphTag]
97
98 // [kotlin_RouteManager_getGraphTag]
99 // Get current graph tag
100 val currentGraphTag = manager.getGraphTag()
101 println("Current graph tag: $currentGraphTag")
102 // [kotlin_RouteManager_getGraphTag]
103
104 // [kotlin_RouteManager_getGraphTags]
105 // Get all graph tags
106 val graphTags = manager.getGraphTags()
107 println("Available graph tags: $graphTags")
108 // [kotlin_RouteManager_getGraphTags]
109
110 // Create test points
111 val startPoint = Point(10.0, 20.0)
112 val endPoint = Point(50.0, 60.0)
113 val startLocationPoint = LocationPoint(startPoint, 12345, 1)
114 val endLocationPoint = LocationPoint(endPoint, 12345, 1)
115
116 // [kotlin_RouteManager_makeRoute]
117 // Make route from point to point
118 val routePath = manager.makeRoute(startLocationPoint, endLocationPoint)
119 println("Route created with length: ${routePath.length} meters")
120 // [kotlin_RouteManager_makeRoute]
121
122 // [kotlin_RouteManager_makeRoutes]
123 // Make routes to multiple destinations
124 val destinations = listOf(
125 LocationPoint(Point(30.0, 40.0), 12345, 1),
126 LocationPoint(Point(70.0, 80.0), 12345, 1)
127 )
128 val routePaths = manager.makeRoutes(startLocationPoint, destinations)
129 println("Created ${routePaths.size} routes")
130 // [kotlin_RouteManager_makeRoutes]
131
132 // [kotlin_RouteManager_setTarget]
133 // Set target point
134 manager.setTarget(endLocationPoint)
135 // [kotlin_RouteManager_setTarget]
136
137 // [kotlin_RouteManager_addTarget]
138 // Add additional target point
139 val additionalTarget = LocationPoint(Point(90.0, 100.0), 12345, 1)
140 manager.addTarget(additionalTarget)
141 // [kotlin_RouteManager_addTarget]
142
143 // [kotlin_RouteManager_cancelTarget]
144 // Cancel current target
145 manager.cancelTarget()
146 // [kotlin_RouteManager_cancelTarget]
147
148 // [kotlin_RouteManager_clearTargets]
149 // Clear all targets
150 manager.clearTargets()
151 // [kotlin_RouteManager_clearTargets]
152 }
153
154 /**
155 * Demonstrate RoutePath class methods
156 */
157 fun demonstrateRoutePathUsage(paths: List<RoutePath>) {
158 paths.forEachIndexed { index, path ->
159 println("=== Route Path ${index + 1} ===")
160
161 // [kotlin_RoutePath_getLength]
162 // Get route length
163 val length = path.length
164 println("Route length: $length meters")
165 // [kotlin_RoutePath_getLength]
166
167 // [kotlin_RoutePath_getPoints]
168 // Get route points
169 val points = path.points
170 println("Route has ${points.size} points")
171 points.forEach { point ->
172 demonstrateLocationPointUsage(point)
173 }
174 // [kotlin_RoutePath_getPoints]
175
176 // [kotlin_RoutePath_getEvents]
177 // Get route events
178 val events = path.events
179 println("Route has ${events.size} events")
180 events.forEach { event ->
181 demonstrateRouteEventUsage(event)
182 }
183 // [kotlin_RoutePath_getEvents]
184
185 // [kotlin_RoutePath_head]
186 // Get head of route (first 10 meters)
187 val headPath = path.head(10.0)
188 headPath?.let {
189 println("Head path length: ${it.length} meters")
190 }
191 // [kotlin_RoutePath_head]
192
193 // [kotlin_RoutePath_tail]
194 // Get tail of route (remaining after 10 meters)
195 val tailPath = path.tail(10.0)
196 tailPath?.let {
197 println("Tail path length: ${it.length} meters")
198 }
199 // [kotlin_RoutePath_tail]
200 }
201 }
202
203 /**
204 * Demonstrate RouteEvent class methods
205 */
206 fun demonstrateRouteEventUsage(event: RouteEvent) {
207 // [kotlin_RouteEvent_getType]
208 // Get event type
209 val type = event.type
210 println("Event type: $type")
211 // [kotlin_RouteEvent_getType]
212
213 // [kotlin_RouteEvent_getValue]
214 // Get event value (angle for turns, sublocation ID for transitions)
215 val value = event.value
216 println("Event value: $value")
217 // [kotlin_RouteEvent_getValue]
218
219 // [kotlin_RouteEvent_getDistance]
220 // Get distance from route start
221 val distance = event.distance
222 println("Event distance: $distance meters")
223 // [kotlin_RouteEvent_getDistance]
224
225 // [kotlin_RouteEvent_constructor]
226 // Create new RouteEvent
227 val newEvent = RouteEvent(type, value, distance)
228 println("Created new RouteEvent: $newEvent")
229 // [kotlin_RouteEvent_constructor]
230 }
231
232 /**
233 * Demonstrate RouteEventType enum values
234 */
235 fun demonstrateRouteEventTypes() {
236 // [kotlin_RouteEventType_values]
237 // Get all route event type values
238 val types = RouteEventType.values()
239 println("Available route event types:")
240 types.forEach { type ->
241 println(" - $type")
242 }
243 // [kotlin_RouteEventType_values]
244 }
245
246 /**
247 * Demonstrate LocationPoint class methods
248 */
249 fun demonstrateLocationPointUsage(locationPoint: LocationPoint) {
250 // [kotlin_LocationPoint_getPoint]
251 // Get point coordinates
252 val point = locationPoint.point
253 point?.let { demonstratePointUsage(it) }
254 // [kotlin_LocationPoint_getPoint]
255
256 // [kotlin_LocationPoint_getLocationId]
257 // Get location ID
258 val locationId = locationPoint.locationId
259 println("Location ID: $locationId")
260 // [kotlin_LocationPoint_getLocationId]
261
262 // [kotlin_LocationPoint_getSublocationId]
263 // Get sublocation ID
264 val sublocationId = locationPoint.sublocationId
265 println("Sublocation ID: $sublocationId")
266 // [kotlin_LocationPoint_getSublocationId]
267
268 // [kotlin_LocationPoint_constructor]
269 // Create new LocationPoint
270 val newPoint = Point(15.0, 25.0)
271 val newLocationPoint = LocationPoint(newPoint, locationId, sublocationId)
272 println("Created new LocationPoint: $newLocationPoint")
273 // [kotlin_LocationPoint_constructor]
274 }
275
276 /**
277 * Demonstrate Point class methods
278 */
279 fun demonstratePointUsage(point: Point) {
280 // [kotlin_Point_getX]
281 // Get X coordinate
282 val x = point.x
283 println("Point X: $x")
284 // [kotlin_Point_getX]
285
286 // [kotlin_Point_getY]
287 // Get Y coordinate
288 val y = point.y
289 println("Point Y: $y")
290 // [kotlin_Point_getY]
291
292 // [kotlin_Point_constructor]
293 // Create new Point
294 val newPoint = Point(x, y)
295 println("Created new Point: $newPoint")
296 // [kotlin_Point_constructor]
297 }
298
299 /**
300 * Demonstrate advanced routing features
301 */
302 fun demonstrateAdvancedRoutingFeatures() {
303 println("=== Advanced Routing Features ===")
304
305 val manager = routeManager ?: return
306
307 // Create multiple routes with different graph tags
308 val graphTags = listOf("main", "elevator", "stairs")
309
310 graphTags.forEach { tag ->
311 // [kotlin_RouteManager_setGraphTag_2]
312 // Set different graph tag for routing
313 manager.setGraphTag(tag)
314 // [kotlin_RouteManager_setGraphTag_2]
315
316 println("Routing with graph tag: $tag")
317
318 // Create test route
319 val startPoint = Point(0.0, 0.0)
320 val endPoint = Point(100.0, 100.0)
321 val startLocationPoint = LocationPoint(startPoint, 12345, 1)
322 val endLocationPoint = LocationPoint(endPoint, 12345, 1)
323
324 // [kotlin_RouteManager_makeRoute_2]
325 // Make route with specific graph tag
326 val routePath = manager.makeRoute(startLocationPoint, endLocationPoint)
327 // [kotlin_RouteManager_makeRoute_2]
328
329 println("Route with tag '$tag' has length: ${routePath.length} meters")
330 }
331 }
332
333 /**
334 * Demonstrate route planning simulation
335 */
336 suspend fun demonstrateRoutePlanningSimulation() {
337 println("=== Route Planning Simulation ===")
338
339 val manager = routeManager ?: return
340
341 // Create multiple target points
342 val targets = listOf(
343 LocationPoint(Point(50.0, 50.0), 12345, 1),
344 LocationPoint(Point(100.0, 100.0), 12345, 1),
345 LocationPoint(Point(150.0, 150.0), 12345, 1)
346 )
347
348 // [kotlin_RouteManager_setTarget_2]
349 // Set first target
350 manager.setTarget(targets[0])
351 // [kotlin_RouteManager_setTarget_2]
352
353 println("Set target 1: ${targets[0]}")
354 delay(1000)
355
356 // [kotlin_RouteManager_addTarget_2]
357 // Add additional targets
358 targets.drop(1).forEachIndexed { index, target ->
359 manager.addTarget(target)
360 println("Added target ${index + 2}: $target")
361 delay(1000)
362 }
363 // [kotlin_RouteManager_addTarget_2]
364
365 // [kotlin_RouteManager_cancelTarget_2]
366 // Cancel current target
367 manager.cancelTarget()
368 println("Cancelled current target")
369 // [kotlin_RouteManager_cancelTarget_2]
370
371 delay(1000)
372
373 // [kotlin_RouteManager_clearTargets_2]
374 // Clear all targets
375 manager.clearTargets()
376 println("Cleared all targets")
377 // [kotlin_RouteManager_clearTargets_2]
378 }
379
380 /**
381 * Clean up resources
382 */
383 fun cleanup() {
384 val manager = routeManager
385 val listener = routeListener
386 if (manager != null && listener != null) {
387 // [kotlin_RouteManager_removeRouteListener]
388 // Remove route listener
389 manager.removeRouteListener(listener)
390 // [kotlin_RouteManager_removeRouteListener]
391 }
392 }
393
394 /**
395 * Main demonstration method
396 */
397 suspend fun runExample() {
398 println("=== RouteManager Example ===")
399
400 demonstrateRouteManagerMethods()
401 demonstrateRouteEventTypes()
402 demonstrateAdvancedRoutingFeatures()
403 demonstrateRoutePlanningSimulation()
404
405 // Wait a bit for route updates
406 delay(3000)
407
408 cleanup()
409 println("=== Example completed ===")
410 }
411}
412
413/**
414 * Function to run the example
415 */
416fun main() = runBlocking {
417 val example = RouteManagerExample()
418 example.runExample()
419}