1package com.navigine.examples
3import com.navigine.idl.java.*
6 * NavigationManager usage example for Kotlin
7 * Demonstrates working with navigation, positioning, and location tracking
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
17 setupPositionListener()
21 * Initialize SDK and get managers
23 private fun initializeSdk() {
25 // [kotlin_NavigineSdk_getInstance]
27 sdk = NavigineSdk.getInstance()
28 // [kotlin_NavigineSdk_getInstance]
30 // [kotlin_NavigineSdk_setUserHash]
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [kotlin_NavigineSdk_setUserHash]
35 // [kotlin_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [kotlin_NavigineSdk_setServer]
40 // [kotlin_NavigineSdk_getLocationManager]
41 // Get LocationManager for working with locations
42 locationManager = sdk?.getLocationManager()
43 // [kotlin_NavigineSdk_getLocationManager]
45 // [kotlin_NavigineSdk_getNavigationManager]
46 // Get NavigationManager for working with navigation
47 navigationManager = sdk?.getNavigationManager(locationManager)
48 // [kotlin_NavigineSdk_getNavigationManager]
50 if (locationManager != null && navigationManager != null) {
51 println("LocationManager and NavigationManager successfully initialized")
53 } catch (e: Exception) {
54 System.err.println("Error initializing SDK: ${e.message}")
59 * Setup position listener
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)
68 // [kotlin_PositionListener_onPositionUpdated]
70 // [kotlin_PositionListener_onPositionError]
71 override fun onPositionError(error: Error) {
72 System.err.println("Position error: ${error.message}")
74 // [kotlin_PositionListener_onPositionError]
79 * Demonstrate NavigationManager methods
81 fun demonstrateNavigationManagerMethods() {
82 val manager = navigationManager ?: run {
83 System.err.println("NavigationManager not initialized")
87 val listener = positionListener ?: return
89 // [kotlin_NavigationManager_addPositionListener]
90 // Add position listener
91 manager.addPositionListener(listener)
92 // [kotlin_NavigationManager_addPositionListener]
94 // [kotlin_NavigationManager_startLogRecording]
95 // Start log recording
96 manager.startLogRecording()
97 // [kotlin_NavigationManager_startLogRecording]
99 // [kotlin_NavigationManager_addCheckPoint]
101 val point = Point(10.0, 20.0)
102 val locationPoint = LocationPoint(point, 12345, 1)
103 manager.addCheckPoint(locationPoint)
104 // [kotlin_NavigationManager_addCheckPoint]
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]
112 // [kotlin_NavigationManager_stopLogRecording]
113 // Stop log recording
114 manager.stopLogRecording()
115 // [kotlin_NavigationManager_stopLogRecording]
119 * Demonstrate Position class methods
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]
128 // [kotlin_Position_getAccuracy]
129 // Get position accuracy
130 val accuracy = position.accuracy
131 println("Position accuracy: $accuracy meters")
132 // [kotlin_Position_getAccuracy]
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]
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]
146 // [kotlin_Position_getLocationPoint]
147 // Get location point (metrics coordinates)
148 val locationPoint = position.locationPoint
149 locationPoint?.let { demonstrateLocationPointUsage(it) }
150 // [kotlin_Position_getLocationPoint]
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]
160 * Demonstrate GlobalPoint class methods
162 fun demonstrateGlobalPointUsage(globalPoint: GlobalPoint) {
163 // [kotlin_GlobalPoint_getLatitude]
165 val latitude = globalPoint.latitude
166 println("Latitude: $latitude")
167 // [kotlin_GlobalPoint_getLatitude]
169 // [kotlin_GlobalPoint_getLongitude]
171 val longitude = globalPoint.longitude
172 println("Longitude: $longitude")
173 // [kotlin_GlobalPoint_getLongitude]
175 // [kotlin_GlobalPoint_constructor]
176 // Create new GlobalPoint
177 val newPoint = GlobalPoint(latitude, longitude)
178 println("Created new GlobalPoint: $newPoint")
179 // [kotlin_GlobalPoint_constructor]
183 * Demonstrate LocationPoint class methods
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]
192 // [kotlin_LocationPoint_getLocationId]
194 val locationId = locationPoint.locationId
195 println("Location ID: $locationId")
196 // [kotlin_LocationPoint_getLocationId]
198 // [kotlin_LocationPoint_getSublocationId]
199 // Get sublocation ID
200 val sublocationId = locationPoint.sublocationId
201 println("Sublocation ID: $sublocationId")
202 // [kotlin_LocationPoint_getSublocationId]
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]
213 * Demonstrate Point class methods
215 fun demonstratePointUsage(point: Point) {
216 // [kotlin_Point_getX]
219 println("Point X: $x")
220 // [kotlin_Point_getX]
222 // [kotlin_Point_getY]
225 println("Point Y: $y")
226 // [kotlin_Point_getY]
228 // [kotlin_Point_constructor]
230 val newPoint = Point(x, y)
231 println("Created new Point: $newPoint")
232 // [kotlin_Point_constructor]
236 * Demonstrate LocationManager integration
238 fun demonstrateLocationManagerIntegration() {
239 val manager = locationManager ?: run {
240 System.err.println("LocationManager not initialized")
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")
251 // [kotlin_LocationManager_setLocationId]
252 // Set location ID for navigation
253 manager.setLocationId(it.id)
254 // [kotlin_LocationManager_setLocationId]
257 // [kotlin_LocationListener_onLocationLoaded]
259 // [kotlin_LocationListener_onLocationUploaded]
260 override fun onLocationUploaded(locationId: Int) {
261 println("Location uploaded: $locationId")
263 // [kotlin_LocationListener_onLocationUploaded]
265 // [kotlin_LocationListener_onLocationFailed]
266 override fun onLocationFailed(locationId: Int, error: Error) {
267 System.err.println("Failed to load location $locationId: ${error.message}")
269 // [kotlin_LocationListener_onLocationFailed]
271 manager.addLocationListener(locationListener)
272 // [kotlin_LocationManager_addLocationListener]
274 // [kotlin_LocationManager_setLocationId]
275 // Set location ID to load
276 manager.setLocationId(12345)
277 // [kotlin_LocationManager_setLocationId]
279 // [kotlin_LocationManager_getLocationId]
280 // Get current location ID
281 val currentLocationId = manager.getLocationId()
282 println("Current location ID: $currentLocationId")
283 // [kotlin_LocationManager_getLocationId]
285 // [kotlin_LocationManager_setLocationUpdateInterval]
286 // Set location update interval
287 manager.setLocationUpdateInterval(300) // 5 minutes
288 // [kotlin_LocationManager_setLocationUpdateInterval]
290 // [kotlin_LocationManager_commitChanges]
292 manager.commitChanges()
293 // [kotlin_LocationManager_commitChanges]
297 * Demonstrate advanced navigation features
299 fun demonstrateAdvancedNavigationFeatures() {
300 println("=== Advanced Navigation Features ===")
302 val manager = navigationManager ?: return
304 // Create multiple check points for path tracking
305 val checkPoints = arrayOf(
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")
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
328 val providers = arrayOf("gps", "network", "fused")
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]}")
340 * Demonstrate position tracking simulation
342 fun demonstratePositionTrackingSimulation() {
343 println("=== Position Tracking Simulation ===")
345 val manager = navigationManager ?: return
347 // [kotlin_NavigationManager_startLogRecording]
348 // Start log recording for position tracking
349 manager.startLogRecording()
350 // [kotlin_NavigationManager_startLogRecording]
352 // Simulate position updates
354 val lat = 55.7558 + (i * 0.001) // Move north
355 val lon = 37.6176 + (i * 0.001) // Move east
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]
363 println("Simulated position ${i + 1}: $globalPoint")
365 Thread.sleep(1000) // Wait 1 second between updates
368 // [kotlin_NavigationManager_stopLogRecording]
369 // Stop log recording
370 manager.stopLogRecording()
371 // [kotlin_NavigationManager_stopLogRecording]
378 navigationManager?.let { manager ->
379 positionListener?.let { listener ->
380 // [kotlin_NavigationManager_removePositionListener]
381 // Remove position listener
382 manager.removePositionListener(listener)
383 // [kotlin_NavigationManager_removePositionListener]
389 * Main demonstration method
392 println("=== NavigationManager Example ===")
394 demonstrateNavigationManagerMethods()
395 demonstrateLocationManagerIntegration()
396 demonstrateAdvancedNavigationFeatures()
397 demonstratePositionTrackingSimulation()
399 // Wait a bit for position updates
403 println("=== Example completed ===")
408 * Function to run the example
411 val example = NavigationManagerExample()