Loading...
Searching...
No Matches
LocationWindowCameraExample.kt
Go to the documentation of this file.
1package com.navigine.examples
2
3import com.navigine.idl.java.LocationWindow
4import com.navigine.idl.java.Camera
5import com.navigine.idl.java.CameraListener
6import com.navigine.idl.java.CameraCallback
7import com.navigine.idl.java.AnimationType
8import com.navigine.idl.java.CameraUpdateReason
9import com.navigine.idl.java.Point
10
11/**
12 * LocationWindowCamera usage example for Kotlin
13 * Demonstrates specific methods: getCamera, setCamera, addCameraListener, removeCameraListener,
14 * flyTo, moveTo, zoomFactor, minZoomFactor, maxZoomFactor
15 */
16class LocationWindowCameraExample {
17 private var locationWindow: LocationWindow? = null
18 private var cameraListener: CameraListener? = null
19
20 constructor() {
21 demonstrateLocationWindowCameraMethods()
22 }
23
24 /**
25 * Demonstrate LocationWindowCamera methods
26 */
27 private fun demonstrateLocationWindowCameraMethods() {
28 println("=== LocationWindowCamera Methods ===")
29
30 demonstrateCameraProperties()
31 demonstrateGetSetCamera()
32 demonstrateCameraListeners()
33 demonstrateFlyToMethod()
34 demonstrateMoveToMethod()
35 demonstrateZoomProperties()
36 demonstrateCameraScenarios()
37 }
38
39 /**
40 * Demonstrate camera properties setup
41 */
42 private fun demonstrateCameraProperties() {
43 println("--- Camera Properties Setup ---")
44
45 if (locationWindow == null) {
46 println("LocationWindow not available yet")
47 return
48 }
49
50 // [kotlin_LocationWindow_minZoomFactor]
51 // Set minimum zoom factor
52 locationWindow!!.minZoomFactor = 10.0f
53 println("Set minimum zoom factor to 10.0 pixels per meter")
54 // [kotlin_LocationWindow_minZoomFactor]
55
56 // [kotlin_LocationWindow_maxZoomFactor]
57 // Set maximum zoom factor
58 locationWindow!!.maxZoomFactor = 1000.0f
59 println("Set maximum zoom factor to 1000.0 pixels per meter")
60 // [kotlin_LocationWindow_maxZoomFactor]
61 }
62
63 /**
64 * Demonstrate getCamera and setCamera methods
65 */
66 private fun demonstrateGetSetCamera() {
67 println("--- getCamera and setCamera Methods ---")
68
69 if (locationWindow == null) {
70 println("LocationWindow not available yet")
71 return
72 }
73
74 // [kotlin_LocationWindow_getCamera]
75 // Get current camera position
76 val currentCamera = locationWindow!!.camera
77 println("Current camera position:")
78 println(" Point: (${currentCamera.point.x}, ${currentCamera.point.y})")
79 println(" Zoom: ${currentCamera.zoom}")
80 println(" Rotation: ${currentCamera.rotation}°")
81 // [kotlin_LocationWindow_getCamera]
82
83 // [kotlin_Camera_constructor]
84 // Create camera with constructor
85 val newPoint = Point(100.0, 200.0)
86 val newCamera = Camera(newPoint, 50.0, 0.0)
87 println("Created camera with point (${newPoint.x}, ${newPoint.y}), zoom 50.0, rotation 0°")
88 // [kotlin_Camera_constructor]
89
90 // [kotlin_Camera_access]
91 // Access camera properties
92 println("Camera properties:")
93 println(" Point: (${newCamera.point.x}, ${newCamera.point.y})")
94 println(" Zoom: ${newCamera.zoom}")
95 println(" Rotation: ${newCamera.rotation}°")
96 // [kotlin_Camera_access]
97
98 // [kotlin_LocationWindow_setCamera]
99 // Set camera position without animation
100 locationWindow!!.camera = newCamera
101 println("Set camera position to (${newPoint.x}, ${newPoint.y}) with zoom 50.0 and rotation 0°")
102 // [kotlin_LocationWindow_setCamera]
103
104 // [kotlin_LocationWindow_setCamera_2]
105 // Set camera position with rotation
106 val rotatedPoint = Point(150.0, 250.0)
107 val rotatedCamera = Camera(rotatedPoint, 75.0, 45.0)
108 locationWindow!!.camera = rotatedCamera
109 println("Set camera position to (${rotatedPoint.x}, ${rotatedPoint.y}) with zoom 75.0 and rotation 45°")
110 // [kotlin_LocationWindow_setCamera_2]
111
112 // Test different camera positions
113 val testCameras = listOf(
114 Camera(Point(0.0, 0.0), 10.0, 0.0), // Overview camera
115 Camera(Point(50.0, 75.0), 500.0, 0.0), // Detail camera
116 Camera(Point(100.0, 150.0), 100.0, 30.0), // Angled camera
117 )
118
119 testCameras.forEachIndexed { i, camera ->
120 locationWindow!!.camera = camera
121 println("Test camera $i: Point (${camera.point.x}, ${camera.point.y}), Zoom ${camera.zoom}, Rotation ${camera.rotation}°")
122 }
123 }
124
125 /**
126 * Demonstrate camera listener methods
127 */
128 private fun demonstrateCameraListeners() {
129 println("--- Camera Listener Methods ---")
130
131 if (locationWindow == null) {
132 println("LocationWindow not available yet")
133 return
134 }
135
136 // [kotlin_LocationWindow_addCameraListener]
137 // Add camera listener
138 cameraListener = CameraListenerImpl()
139 locationWindow!!.addCameraListener(cameraListener!!)
140 println("Added camera listener")
141 // [kotlin_LocationWindow_addCameraListener]
142
143 // [kotlin_LocationWindow_removeCameraListener]
144 // Remove camera listener
145 locationWindow!!.removeCameraListener(cameraListener!!)
146 cameraListener = null
147 println("Removed camera listener")
148 // [kotlin_LocationWindow_removeCameraListener]
149
150 // Test multiple listeners
151 val listeners = listOf(
152 CameraListenerImpl(),
153 CameraListenerImpl(),
154 CameraListenerImpl(),
155 )
156
157 listeners.forEachIndexed { i, listener ->
158 locationWindow!!.addCameraListener(listener)
159 println("Added camera listener $i")
160 }
161
162 listeners.forEachIndexed { i, listener ->
163 locationWindow!!.removeCameraListener(listener)
164 println("Removed camera listener $i")
165 }
166 }
167
168 /**
169 * Demonstrate flyTo method
170 */
171 private fun demonstrateFlyToMethod() {
172 println("--- flyTo Method ---")
173
174 if (locationWindow == null) {
175 println("LocationWindow not available yet")
176 return
177 }
178
179 // [kotlin_LocationWindow_flyTo]
180 // Fly to position with smooth animation
181 val targetPoint = Point(150.0, 250.0)
182 val targetCamera = Camera(targetPoint, 75.0, 45.0)
183 val callback = CameraCallbackImpl()
184 locationWindow!!.flyTo(targetCamera, 2000, callback)
185 println("Started fly to animation to point (${targetPoint.x}, ${targetPoint.y})")
186 // [kotlin_LocationWindow_flyTo]
187
188 // [kotlin_LocationWindow_flyTo_2]
189 // Fly to another position with different duration
190 val targetPoint2 = Point(300.0, 400.0)
191 val targetCamera2 = Camera(targetPoint2, 25.0, 180.0)
192 val callback2 = CameraCallbackImpl()
193 locationWindow!!.flyTo(targetCamera2, 3000, callback2)
194 println("Started fly to animation to point (${targetPoint2.x}, ${targetPoint2.y}) with 3 second duration")
195 // [kotlin_LocationWindow_flyTo_2]
196
197 // Test fly to with different parameters
198 val flyToTests = listOf(
199 mapOf("point" to Point(50.0, 50.0), "zoom" to 100.0, "rotation" to 0.0, "duration" to 1000),
200 mapOf("point" to Point(200.0, 300.0), "zoom" to 200.0, "rotation" to 90.0, "duration" to 1500),
201 mapOf("point" to Point(400.0, 100.0), "zoom" to 50.0, "rotation" to 270.0, "duration" to 2500),
202 )
203
204 flyToTests.forEachIndexed { i, test ->
205 val point = test["point"] as Point
206 val zoom = test["zoom"] as Double
207 val rotation = test["rotation"] as Double
208 val duration = test["duration"] as Int
209
210 val camera = Camera(point, zoom, rotation)
211 val testCallback = CameraCallbackImpl()
212 locationWindow!!.flyTo(camera, duration, testCallback)
213 println("Fly to test $i: Point (${point.x}, ${point.y}), Zoom $zoom, Rotation ${rotation}°, Duration ${duration}ms")
214 }
215 }
216
217 /**
218 * Demonstrate moveTo method
219 */
220 private fun demonstrateMoveToMethod() {
221 println("--- moveTo Method ---")
222
223 if (locationWindow == null) {
224 println("LocationWindow not available yet")
225 return
226 }
227
228 // [kotlin_LocationWindow_moveTo]
229 // Move to position with linear animation
230 val targetPoint = Point(200.0, 300.0)
231 val targetCamera = Camera(targetPoint, 100.0, 90.0)
232 val callback = CameraCallbackImpl()
233 locationWindow!!.moveTo(targetCamera, 1500, AnimationType.LINEAR, callback)
234 println("Started move to with linear animation")
235 // [kotlin_LocationWindow_moveTo]
236
237 // [kotlin_LocationWindow_moveTo_2]
238 // Move to position with cubic animation
239 val callback2 = CameraCallbackImpl()
240 locationWindow!!.moveTo(targetCamera, 1500, AnimationType.CUBIC, callback2)
241 println("Started move to with cubic animation")
242 // [kotlin_LocationWindow_moveTo_2]
243
244 // [kotlin_LocationWindow_moveTo_3]
245 // Move to position with sine animation
246 val callback3 = CameraCallbackImpl()
247 locationWindow!!.moveTo(targetCamera, 1500, AnimationType.SINE, callback3)
248 println("Started move to with sine animation")
249 // [kotlin_LocationWindow_moveTo_3]
250
251 // [kotlin_LocationWindow_moveTo_4]
252 // Move to position without animation
253 val instantPoint = Point(300.0, 400.0)
254 val instantCamera = Camera(instantPoint, 25.0, 180.0)
255 val instantCallback = CameraCallbackImpl()
256 locationWindow!!.moveTo(instantCamera, 0, AnimationType.NONE, instantCallback)
257 println("Executed instant move to position (${instantPoint.x}, ${instantPoint.y})")
258 // [kotlin_LocationWindow_moveTo_4]
259
260 // Test move to with different animation types
261 val animationTypes = listOf(
262 AnimationType.LINEAR,
263 AnimationType.CUBIC,
264 AnimationType.QUINT,
265 AnimationType.SINE,
266 AnimationType.NONE,
267 )
268
269 animationTypes.forEachIndexed { i, animationType ->
270 val testPoint = Point(100.0 + i * 50.0, 200.0 + i * 50.0)
271 val testCamera = Camera(testPoint, 50.0 + i * 10.0, i * 30.0)
272 val testCallback = CameraCallbackImpl()
273 locationWindow!!.moveTo(testCamera, 1000, animationType, testCallback)
274 println("Move to test $i: Animation ${animationType.name}, Point (${testPoint.x}, ${testPoint.y})")
275 }
276 }
277
278 /**
279 * Demonstrate zoom properties
280 */
281 private fun demonstrateZoomProperties() {
282 println("--- Zoom Properties ---")
283
284 if (locationWindow == null) {
285 println("LocationWindow not available yet")
286 return
287 }
288
289 // [kotlin_LocationWindow_getZoomFactor]
290 // Get current zoom factor
291 val currentZoom = locationWindow!!.zoomFactor
292 println("Current zoom factor: $currentZoom pixels per meter")
293 // [kotlin_LocationWindow_getZoomFactor]
294
295 // [kotlin_LocationWindow_setZoomFactor]
296 // Set new zoom factor
297 locationWindow!!.zoomFactor = 200.0f
298 println("Set zoom factor to 200.0 pixels per meter")
299 // [kotlin_LocationWindow_setZoomFactor]
300
301 // [kotlin_LocationWindow_getMinZoomFactor]
302 // Get minimum zoom factor
303 val minZoom = locationWindow!!.minZoomFactor
304 println("Minimum zoom factor: $minZoom pixels per meter")
305 // [kotlin_LocationWindow_getMinZoomFactor]
306
307 // [kotlin_LocationWindow_getMaxZoomFactor]
308 // Get maximum zoom factor
309 val maxZoom = locationWindow!!.maxZoomFactor
310 println("Maximum zoom factor: $maxZoom pixels per meter")
311 // [kotlin_LocationWindow_getMaxZoomFactor]
312
313 // Test zoom factor changes
314 val zoomLevels = listOf(50.0f, 100.0f, 150.0f, 300.0f, 500.0f, 800.0f)
315
316 zoomLevels.forEach { zoom ->
317 locationWindow!!.zoomFactor = zoom
318 println("Set zoom factor to $zoom pixels per meter")
319
320 // Verify the change
321 val actualZoom = locationWindow!!.zoomFactor
322 println("Actual zoom factor: $actualZoom pixels per meter")
323 }
324 }
325
326 /**
327 * Demonstrate camera scenarios
328 */
329 private fun demonstrateCameraScenarios() {
330 println("--- Camera Scenarios ---")
331
332 if (locationWindow == null) {
333 println("LocationWindow not available yet")
334 return
335 }
336
337 // [kotlin_LocationWindow_camera_scenarios]
338 // Create cameras for different scenarios
339 val overviewCamera = Camera(Point(0.0, 0.0), 10.0, 0.0)
340 val detailCamera = Camera(Point(50.0, 75.0), 500.0, 0.0)
341 val angledCamera = Camera(Point(100.0, 150.0), 100.0, 30.0)
342
343 println("Created cameras for different scenarios:")
344 println(" Overview camera: zoom ${overviewCamera.zoom}")
345 println(" Detail camera: zoom ${detailCamera.zoom}")
346 println(" Angled camera: rotation ${angledCamera.rotation}°")
347 // [kotlin_LocationWindow_camera_scenarios]
348
349 // Test scenario transitions
350 val scenarios = listOf(overviewCamera, detailCamera, angledCamera)
351 val scenarioNames = listOf("Overview", "Detail", "Angled")
352
353 scenarios.forEachIndexed { i, scenario ->
354 val name = scenarioNames[i]
355 val scenarioCallback = CameraCallbackImpl()
356 locationWindow!!.moveTo(scenario, 2000, AnimationType.CUBIC, scenarioCallback)
357 println("Transitioning to $name scenario")
358 }
359 }
360
361 /**
362 * Cleanup resources
363 */
364 fun cleanup() {
365 cameraListener?.let { listener ->
366 locationWindow?.removeCameraListener(listener)
367 cameraListener = null
368 println("Camera listener removed")
369 }
370 }
371}
372
373/**
374 * Camera listener implementation
375 */
376class CameraListenerImpl : CameraListener {
377 // [kotlin_CameraListener_onCameraPositionChanged]
378 override fun onCameraPositionChanged(reason: CameraUpdateReason, finished: Boolean) {
379 val reasonText = if (reason == CameraUpdateReason.GESTURES) "user gestures" else "application"
380 val statusText = if (finished) "finished" else "in progress"
381
382 println("Camera position changed: $reasonText, status: $statusText")
383
384 if (finished) {
385 println("Camera movement completed")
386 }
387 }
388 // [kotlin_CameraListener_onCameraPositionChanged]
389}
390
391/**
392 * Camera callback implementation
393 */
394class CameraCallbackImpl : CameraCallback {
395 override fun onMoveFinished(completed: Boolean) {
396 if (completed) {
397 println("Camera movement completed successfully")
398 } else {
399 println("Camera movement was cancelled")
400 }
401 }
402}
403
404/**
405 * Camera utilities
406 */
407object CameraUtils {
408 /**
409 * Create camera for centered point with given zoom
410 */
411 fun createCenteredCamera(x: Double, y: Double, zoom: Double): Camera {
412 return Camera(Point(x, y), zoom, 0.0)
413 }
414
415 /**
416 * Create camera with rotation
417 */
418 fun createRotatedCamera(x: Double, y: Double, zoom: Double, rotation: Double): Camera {
419 return Camera(Point(x, y), zoom, rotation)
420 }
421
422 /**
423 * Validate camera position
424 */
425 fun isValidCamera(camera: Camera): Boolean {
426 return camera.zoom > 0 &&
427 camera.rotation >= 0 && camera.rotation < 360
428 }
429
430 /**
431 * Create camera for showing area
432 */
433 fun createAreaCamera(centerX: Double, centerY: Double, width: Double, height: Double): Camera {
434 // Calculate zoom based on area size
435 val zoom = 100.0 / maxOf(width, height)
436 return Camera(Point(centerX, centerY), zoom, 0.0)
437 }
438
439 /**
440 * Create camera for showing path
441 */
442 fun createPathCamera(points: List<Point>): Camera {
443 if (points.isEmpty()) {
444 return Camera(Point(0.0, 0.0), 100.0, 0.0)
445 }
446
447 // Find path center
448 val centerX = points.map { it.x }.average()
449 val centerY = points.map { it.y }.average()
450
451 // Calculate area size
452 val minX = points.minOf { it.x }
453 val minY = points.minOf { it.y }
454 val maxX = points.maxOf { it.x }
455 val maxY = points.maxOf { it.y }
456
457 val width = maxX - minX
458 val height = maxY - minY
459
460 return createAreaCamera(centerX, centerY, width, height)
461 }
462
463 /**
464 * Create camera for showing multiple points
465 */
466 fun createMultiPointCamera(points: List<Point>, padding: Double = 10.0): Camera {
467 if (points.isEmpty()) {
468 return Camera(Point(0.0, 0.0), 100.0, 0.0)
469 }
470
471 // Find center
472 val centerX = points.map { it.x }.average()
473 val centerY = points.map { it.y }.average()
474
475 // Calculate area size with padding
476 val minX = points.minOf { it.x } - padding
477 val minY = points.minOf { it.y } - padding
478 val maxX = points.maxOf { it.x } + padding
479 val maxY = points.maxOf { it.y } + padding
480
481 val width = maxX - minX
482 val height = maxY - minY
483
484 return createAreaCamera(centerX, centerY, width, height)
485 }
486}
487
488/**
489 * Extensions for convenient camera operations
490 */
491fun LocationWindow.zoomTo(point: Point, zoom: Double, duration: Int = 1000) {
492 val camera = Camera(point, zoom, 0.0)
493 val callback = CameraCallbackImpl()
494 moveTo(camera, duration, AnimationType.CUBIC, callback)
495}
496
497fun LocationWindow.panTo(point: Point, duration: Int = 1000) {
498 val currentCamera = camera
499 val newCamera = Camera(point, currentCamera.zoom, currentCamera.rotation)
500 val callback = CameraCallbackImpl()
501 moveTo(newCamera, duration, AnimationType.LINEAR, callback)
502}
503
504fun LocationWindow.rotateTo(rotation: Double, duration: Int = 1000) {
505 val currentCamera = camera
506 val newCamera = Camera(currentCamera.point, currentCamera.zoom, rotation)
507 val callback = CameraCallbackImpl()
508 moveTo(newCamera, duration, AnimationType.SINE, callback)
509}
510
511/**
512 * Usage example
513 */
514fun main() {
515 // Create example instance
516 val example = LocationWindowCameraExample()
517
518 // Assume we have LocationWindow
519 // val locationWindow: LocationWindow = getLocationWindow()
520 // example.locationWindow = locationWindow
521
522 // Demonstrate methods
523 println("=== LocationWindowCamera Examples ===")
524
525 // Cleanup
526 example.cleanup()
527
528 println("Examples ready to use!")
529}