Loading...
Searching...
No Matches
NavigationManagerExample.kt
Go to the documentation of this file.
1package com.navigine.examples
2
3import com.navigine.idl.java.*
4
5/**
6 * NavigationManager usage example for Kotlin
7 * Demonstrates working with navigation, positioning, and location tracking
8 */
9class NavigationManagerExample {
10 private var sdk: NavigineSdk? = null
11 private var locationManager: LocationManager? = null
12 private var navigationManager: NavigationManager? = null
13 private var positionListener: PositionListener? = null
14
15 init {
16 initializeSdk()
17 setupPositionListener()
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 if (locationManager != null && navigationManager != null) {
51 println("LocationManager and NavigationManager successfully initialized")
52 }
53 } catch (e: Exception) {
54 System.err.println("Error initializing SDK: ${e.message}")
55 }
56 }
57
58 /**
59 * Setup position listener
60 */
61 private fun setupPositionListener() {
62 positionListener = object : PositionListener() {
63 // [kotlin_PositionListener_onPositionUpdated]
64 override fun onPositionUpdated(position: Position) {
65 println("Position updated successfully")
66 demonstratePositionUsage(position)
67 }
68 // [kotlin_PositionListener_onPositionUpdated]
69
70 // [kotlin_PositionListener_onPositionError]
71 override fun onPositionError(error: Error) {
72 System.err.println("Position error: ${error.message}")
73 }
74 // [kotlin_PositionListener_onPositionError]
75 }
76 }
77
78 /**
79 * Demonstrate NavigationManager methods
80 */
81 fun demonstrateNavigationManagerMethods() {
82 val manager = navigationManager ?: run {
83 System.err.println("NavigationManager not initialized")
84 return
85 }
86
87 val listener = positionListener ?: return
88
89 // [kotlin_NavigationManager_addPositionListener]
90 // Add position listener
91 manager.addPositionListener(listener)
92 // [kotlin_NavigationManager_addPositionListener]
93
94 // [kotlin_NavigationManager_startLogRecording]
95 // Start log recording
96 manager.startLogRecording()
97 // [kotlin_NavigationManager_startLogRecording]
98
99 // [kotlin_NavigationManager_addCheckPoint]
100 // Add check point
101 val point = Point(10.0, 20.0)
102 val locationPoint = LocationPoint(point, 12345, 1)
103 manager.addCheckPoint(locationPoint)
104 // [kotlin_NavigationManager_addCheckPoint]
105
106 // [kotlin_NavigationManager_addLocationMeasurement]
107 // Add location measurement
108 val globalPoint = GlobalPoint(55.7558, 37.6176) // Moscow coordinates
109 manager.addLocationMeasurement(globalPoint, 5.0f, "gps")
110 // [kotlin_NavigationManager_addLocationMeasurement]
111
112 // [kotlin_NavigationManager_stopLogRecording]
113 // Stop log recording
114 manager.stopLogRecording()
115 // [kotlin_NavigationManager_stopLogRecording]
116 }
117
118 /**
119 * Demonstrate Position class methods
120 */
121 fun demonstratePositionUsage(position: Position) {
122 // [kotlin_Position_getPoint]
123 // Get global point (WGS84 coordinates)
124 val globalPoint = position.point
125 globalPoint?.let { demonstrateGlobalPointUsage(it) }
126 // [kotlin_Position_getPoint]
127
128 // [kotlin_Position_getAccuracy]
129 // Get position accuracy
130 val accuracy = position.accuracy
131 println("Position accuracy: $accuracy meters")
132 // [kotlin_Position_getAccuracy]
133
134 // [kotlin_Position_getHeading]
135 // Get heading (angle of rotation about the -Z axis in radians)
136 val heading = position.heading
137 heading?.let { println("Heading: $it radians") }
138 // [kotlin_Position_getHeading]
139
140 // [kotlin_Position_getHeadingAccuracy]
141 // Get heading accuracy
142 val headingAccuracy = position.headingAccuracy
143 headingAccuracy?.let { println("Heading accuracy: $it radians") }
144 // [kotlin_Position_getHeadingAccuracy]
145
146 // [kotlin_Position_getLocationPoint]
147 // Get location point (metrics coordinates)
148 val locationPoint = position.locationPoint
149 locationPoint?.let { demonstrateLocationPointUsage(it) }
150 // [kotlin_Position_getLocationPoint]
151
152 // [kotlin_Position_getLocationHeading]
153 // Get location heading (with respect to sublocation north)
154 val locationHeading = position.locationHeading
155 locationHeading?.let { println("Location heading: $it radians") }
156 // [kotlin_Position_getLocationHeading]
157 }
158
159 /**
160 * Demonstrate GlobalPoint class methods
161 */
162 fun demonstrateGlobalPointUsage(globalPoint: GlobalPoint) {
163 // [kotlin_GlobalPoint_getLatitude]
164 // Get latitude
165 val latitude = globalPoint.latitude
166 println("Latitude: $latitude")
167 // [kotlin_GlobalPoint_getLatitude]
168
169 // [kotlin_GlobalPoint_getLongitude]
170 // Get longitude
171 val longitude = globalPoint.longitude
172 println("Longitude: $longitude")
173 // [kotlin_GlobalPoint_getLongitude]
174
175 // [kotlin_GlobalPoint_constructor]
176 // Create new GlobalPoint
177 val newPoint = GlobalPoint(latitude, longitude)
178 println("Created new GlobalPoint: $newPoint")
179 // [kotlin_GlobalPoint_constructor]
180 }
181
182 /**
183 * Demonstrate LocationPoint class methods
184 */
185 fun demonstrateLocationPointUsage(locationPoint: LocationPoint) {
186 // [kotlin_LocationPoint_getPoint]
187 // Get point coordinates
188 val point = locationPoint.point
189 point?.let { demonstratePointUsage(it) }
190 // [kotlin_LocationPoint_getPoint]
191
192 // [kotlin_LocationPoint_getLocationId]
193 // Get location ID
194 val locationId = locationPoint.locationId
195 println("Location ID: $locationId")
196 // [kotlin_LocationPoint_getLocationId]
197
198 // [kotlin_LocationPoint_getSublocationId]
199 // Get sublocation ID
200 val sublocationId = locationPoint.sublocationId
201 println("Sublocation ID: $sublocationId")
202 // [kotlin_LocationPoint_getSublocationId]
203
204 // [kotlin_LocationPoint_constructor]
205 // Create new LocationPoint
206 val newPoint = Point(15.0, 25.0)
207 val newLocationPoint = LocationPoint(newPoint, locationId, sublocationId)
208 println("Created new LocationPoint: $newLocationPoint")
209 // [kotlin_LocationPoint_constructor]
210 }
211
212 /**
213 * Demonstrate Point class methods
214 */
215 fun demonstratePointUsage(point: Point) {
216 // [kotlin_Point_getX]
217 // Get X coordinate
218 val x = point.x
219 println("Point X: $x")
220 // [kotlin_Point_getX]
221
222 // [kotlin_Point_getY]
223 // Get Y coordinate
224 val y = point.y
225 println("Point Y: $y")
226 // [kotlin_Point_getY]
227
228 // [kotlin_Point_constructor]
229 // Create new Point
230 val newPoint = Point(x, y)
231 println("Created new Point: $newPoint")
232 // [kotlin_Point_constructor]
233 }
234
235 /**
236 * Demonstrate LocationManager integration
237 */
238 fun demonstrateLocationManagerIntegration() {
239 val manager = locationManager ?: run {
240 System.err.println("LocationManager not initialized")
241 return
242 }
243
244 // [kotlin_LocationManager_addLocationListener]
245 // Add location listener for navigation integration
246 val locationListener = object : LocationListener() {
247 // [kotlin_LocationListener_onLocationLoaded]
248 override fun onLocationLoaded(location: Location?) {
249 println("Location loaded for navigation")
250 location?.let {
251 // [kotlin_LocationManager_setLocationId]
252 // Set location ID for navigation
253 manager.setLocationId(it.id)
254 // [kotlin_LocationManager_setLocationId]
255 }
256 }
257 // [kotlin_LocationListener_onLocationLoaded]
258
259 // [kotlin_LocationListener_onLocationUploaded]
260 override fun onLocationUploaded(locationId: Int) {
261 println("Location uploaded: $locationId")
262 }
263 // [kotlin_LocationListener_onLocationUploaded]
264
265 // [kotlin_LocationListener_onLocationFailed]
266 override fun onLocationFailed(locationId: Int, error: Error) {
267 System.err.println("Failed to load location $locationId: ${error.message}")
268 }
269 // [kotlin_LocationListener_onLocationFailed]
270 }
271 manager.addLocationListener(locationListener)
272 // [kotlin_LocationManager_addLocationListener]
273
274 // [kotlin_LocationManager_setLocationId]
275 // Set location ID to load
276 manager.setLocationId(12345)
277 // [kotlin_LocationManager_setLocationId]
278
279 // [kotlin_LocationManager_getLocationId]
280 // Get current location ID
281 val currentLocationId = manager.getLocationId()
282 println("Current location ID: $currentLocationId")
283 // [kotlin_LocationManager_getLocationId]
284
285 // [kotlin_LocationManager_setLocationUpdateInterval]
286 // Set location update interval
287 manager.setLocationUpdateInterval(300) // 5 minutes
288 // [kotlin_LocationManager_setLocationUpdateInterval]
289
290 // [kotlin_LocationManager_commitChanges]
291 // Commit changes
292 manager.commitChanges()
293 // [kotlin_LocationManager_commitChanges]
294 }
295
296 /**
297 * Demonstrate advanced navigation features
298 */
299 fun demonstrateAdvancedNavigationFeatures() {
300 println("=== Advanced Navigation Features ===")
301
302 val manager = navigationManager ?: return
303
304 // Create multiple check points for path tracking
305 val checkPoints = arrayOf(
306 Point(0.0, 0.0),
307 Point(10.0, 10.0),
308 Point(20.0, 20.0),
309 Point(30.0, 30.0)
310 )
311
312 checkPoints.forEachIndexed { index, point ->
313 val locationPoint = LocationPoint(point, 12345, 1)
314 // [kotlin_NavigationManager_addCheckPoint]
315 // Add check point for path tracking
316 manager.addCheckPoint(locationPoint)
317 // [kotlin_NavigationManager_addCheckPoint]
318 println("Added check point ${index + 1}: $locationPoint")
319 }
320
321 // Add location measurements with different providers
322 val measurements = arrayOf(
323 GlobalPoint(55.7558, 37.6176), // Moscow
324 GlobalPoint(40.7128, -74.0060), // New York
325 GlobalPoint(51.5074, -0.1278) // London
326 )
327
328 val providers = arrayOf("gps", "network", "fused")
329
330 measurements.forEachIndexed { index, globalPoint ->
331 // [kotlin_NavigationManager_addLocationMeasurement]
332 // Add location measurement with different providers
333 manager.addLocationMeasurement(globalPoint, 3.0f + index, providers[index % providers.size])
334 // [kotlin_NavigationManager_addLocationMeasurement]
335 println("Added measurement ${index + 1} with provider: ${providers[index % providers.size]}")
336 }
337 }
338
339 /**
340 * Demonstrate position tracking simulation
341 */
342 fun demonstratePositionTrackingSimulation() {
343 println("=== Position Tracking Simulation ===")
344
345 val manager = navigationManager ?: return
346
347 // [kotlin_NavigationManager_startLogRecording]
348 // Start log recording for position tracking
349 manager.startLogRecording()
350 // [kotlin_NavigationManager_startLogRecording]
351
352 // Simulate position updates
353 repeat(5) { i ->
354 val lat = 55.7558 + (i * 0.001) // Move north
355 val lon = 37.6176 + (i * 0.001) // Move east
356
357 val globalPoint = GlobalPoint(lat, lon)
358 // [kotlin_NavigationManager_addLocationMeasurement]
359 // Add simulated location measurement
360 manager.addLocationMeasurement(globalPoint, 2.0f, "simulated")
361 // [kotlin_NavigationManager_addLocationMeasurement]
362
363 println("Simulated position ${i + 1}: $globalPoint")
364
365 Thread.sleep(1000) // Wait 1 second between updates
366 }
367
368 // [kotlin_NavigationManager_stopLogRecording]
369 // Stop log recording
370 manager.stopLogRecording()
371 // [kotlin_NavigationManager_stopLogRecording]
372 }
373
374 /**
375 * Clean up resources
376 */
377 fun cleanup() {
378 navigationManager?.let { manager ->
379 positionListener?.let { listener ->
380 // [kotlin_NavigationManager_removePositionListener]
381 // Remove position listener
382 manager.removePositionListener(listener)
383 // [kotlin_NavigationManager_removePositionListener]
384 }
385 }
386 }
387
388 /**
389 * Main demonstration method
390 */
391 fun runExample() {
392 println("=== NavigationManager Example ===")
393
394 demonstrateNavigationManagerMethods()
395 demonstrateLocationManagerIntegration()
396 demonstrateAdvancedNavigationFeatures()
397 demonstratePositionTrackingSimulation()
398
399 // Wait a bit for position updates
400 Thread.sleep(3000)
401
402 cleanup()
403 println("=== Example completed ===")
404 }
405}
406
407/**
408 * Function to run the example
409 */
410fun main() {
411 val example = NavigationManagerExample()
412 example.runExample()
413}