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 // [kotlin_AnimationType_enum]
262 val animationTypes = listOf(
263 AnimationType.LINEAR,
264 AnimationType.CUBIC,
265 AnimationType.QUINT,
266 AnimationType.SINE,
267 AnimationType.NONE,
268 )
269 // [kotlin_AnimationType_enum]
270
271 animationTypes.forEachIndexed { i, animationType ->
272 val testPoint = Point(100.0 + i * 50.0, 200.0 + i * 50.0)
273 val testCamera = Camera(testPoint, 50.0 + i * 10.0, i * 30.0)
274 val testCallback = CameraCallbackImpl()
275 locationWindow!!.moveTo(testCamera, 1000, animationType, testCallback)
276 println("Move to test $i: Animation ${animationType.name}, Point (${testPoint.x}, ${testPoint.y})")
277 }
278 }
279
280 /**
281 * Demonstrate zoom properties
282 */
283 private fun demonstrateZoomProperties() {
284 println("--- Zoom Properties ---")
285
286 if (locationWindow == null) {
287 println("LocationWindow not available yet")
288 return
289 }
290
291 // [kotlin_LocationWindow_getZoomFactor]
292 // Get current zoom factor
293 val currentZoom = locationWindow!!.zoomFactor
294 println("Current zoom factor: $currentZoom pixels per meter")
295 // [kotlin_LocationWindow_getZoomFactor]
296
297 // [kotlin_LocationWindow_setZoomFactor]
298 // Set new zoom factor
299 locationWindow!!.zoomFactor = 200.0f
300 println("Set zoom factor to 200.0 pixels per meter")
301 // [kotlin_LocationWindow_setZoomFactor]
302
303 // [kotlin_LocationWindow_getMinZoomFactor]
304 // Get minimum zoom factor
305 val minZoom = locationWindow!!.minZoomFactor
306 println("Minimum zoom factor: $minZoom pixels per meter")
307 // [kotlin_LocationWindow_getMinZoomFactor]
308
309 // [kotlin_LocationWindow_getMaxZoomFactor]
310 // Get maximum zoom factor
311 val maxZoom = locationWindow!!.maxZoomFactor
312 println("Maximum zoom factor: $maxZoom pixels per meter")
313 // [kotlin_LocationWindow_getMaxZoomFactor]
314
315 // Test zoom factor changes
316 val zoomLevels = listOf(50.0f, 100.0f, 150.0f, 300.0f, 500.0f, 800.0f)
317
318 zoomLevels.forEach { zoom ->
319 locationWindow!!.zoomFactor = zoom
320 println("Set zoom factor to $zoom pixels per meter")
321
322 // Verify the change
323 val actualZoom = locationWindow!!.zoomFactor
324 println("Actual zoom factor: $actualZoom pixels per meter")
325 }
326 }
327
328 /**
329 * Demonstrate camera scenarios
330 */
331 private fun demonstrateCameraScenarios() {
332 println("--- Camera Scenarios ---")
333
334 if (locationWindow == null) {
335 println("LocationWindow not available yet")
336 return
337 }
338
339 // [kotlin_LocationWindow_camera_scenarios]
340 // Create cameras for different scenarios
341 val overviewCamera = Camera(Point(0.0, 0.0), 10.0, 0.0)
342 val detailCamera = Camera(Point(50.0, 75.0), 500.0, 0.0)
343 val angledCamera = Camera(Point(100.0, 150.0), 100.0, 30.0)
344
345 println("Created cameras for different scenarios:")
346 println(" Overview camera: zoom ${overviewCamera.zoom}")
347 println(" Detail camera: zoom ${detailCamera.zoom}")
348 println(" Angled camera: rotation ${angledCamera.rotation}°")
349 // [kotlin_LocationWindow_camera_scenarios]
350
351 // Test scenario transitions
352 val scenarios = listOf(overviewCamera, detailCamera, angledCamera)
353 val scenarioNames = listOf("Overview", "Detail", "Angled")
354
355 scenarios.forEachIndexed { i, scenario ->
356 val name = scenarioNames[i]
357 val scenarioCallback = CameraCallbackImpl()
358 locationWindow!!.moveTo(scenario, 2000, AnimationType.CUBIC, scenarioCallback)
359 println("Transitioning to $name scenario")
360 }
361 }
362
363 /**
364 * Cleanup resources
365 */
366 fun cleanup() {
367 cameraListener?.let { listener ->
368 locationWindow?.removeCameraListener(listener)
369 cameraListener = null
370 println("Camera listener removed")
371 }
372 }
373}
374
375/**
376 * Camera listener implementation
377 */
378class CameraListenerImpl : CameraListener {
379 // [kotlin_CameraListener_onCameraPositionChanged]
380 override fun onCameraPositionChanged(reason: CameraUpdateReason, finished: Boolean) {
381 val reasonText = if (reason == CameraUpdateReason.GESTURES) "user gestures" else "application"
382 val statusText = if (finished) "finished" else "in progress"
383
384 println("Camera position changed: $reasonText, status: $statusText")
385
386 if (finished) {
387 println("Camera movement completed")
388 }
389 }
390 // [kotlin_CameraListener_onCameraPositionChanged]
391}
392
393/**
394 * Camera callback implementation
395 */
396class CameraCallbackImpl : CameraCallback {
397 // [kotlin_CameraCallback_onMoveFinished]
398 override fun onMoveFinished(completed: Boolean) {
399 if (completed) {
400 println("Camera movement completed successfully")
401 } else {
402 println("Camera movement was cancelled")
403 }
404 }
405 // [kotlin_CameraCallback_onMoveFinished]
406}
407
408/**
409 * Camera utilities
410 */
411object CameraUtils {
412 /**
413 * Create camera for centered point with given zoom
414 */
415 fun createCenteredCamera(x: Double, y: Double, zoom: Double): Camera {
416 return Camera(Point(x, y), zoom, 0.0)
417 }
418
419 /**
420 * Create camera with rotation
421 */
422 fun createRotatedCamera(x: Double, y: Double, zoom: Double, rotation: Double): Camera {
423 return Camera(Point(x, y), zoom, rotation)
424 }
425
426 /**
427 * Validate camera position
428 */
429 fun isValidCamera(camera: Camera): Boolean {
430 return camera.zoom > 0 &&
431 camera.rotation >= 0 && camera.rotation < 360
432 }
433
434 /**
435 * Create camera for showing area
436 */
437 fun createAreaCamera(centerX: Double, centerY: Double, width: Double, height: Double): Camera {
438 // Calculate zoom based on area size
439 val zoom = 100.0 / maxOf(width, height)
440 return Camera(Point(centerX, centerY), zoom, 0.0)
441 }
442
443 /**
444 * Create camera for showing path
445 */
446 fun createPathCamera(points: List<Point>): Camera {
447 if (points.isEmpty()) {
448 return Camera(Point(0.0, 0.0), 100.0, 0.0)
449 }
450
451 // Find path center
452 val centerX = points.map { it.x }.average()
453 val centerY = points.map { it.y }.average()
454
455 // Calculate area size
456 val minX = points.minOf { it.x }
457 val minY = points.minOf { it.y }
458 val maxX = points.maxOf { it.x }
459 val maxY = points.maxOf { it.y }
460
461 val width = maxX - minX
462 val height = maxY - minY
463
464 return createAreaCamera(centerX, centerY, width, height)
465 }
466
467 /**
468 * Create camera for showing multiple points
469 */
470 fun createMultiPointCamera(points: List<Point>, padding: Double = 10.0): Camera {
471 if (points.isEmpty()) {
472 return Camera(Point(0.0, 0.0), 100.0, 0.0)
473 }
474
475 // Find center
476 val centerX = points.map { it.x }.average()
477 val centerY = points.map { it.y }.average()
478
479 // Calculate area size with padding
480 val minX = points.minOf { it.x } - padding
481 val minY = points.minOf { it.y } - padding
482 val maxX = points.maxOf { it.x } + padding
483 val maxY = points.maxOf { it.y } + padding
484
485 val width = maxX - minX
486 val height = maxY - minY
487
488 return createAreaCamera(centerX, centerY, width, height)
489 }
490}
491
492/**
493 * Extensions for convenient camera operations
494 */
495fun LocationWindow.zoomTo(point: Point, zoom: Double, duration: Int = 1000) {
496 val camera = Camera(point, zoom, 0.0)
497 val callback = CameraCallbackImpl()
498 moveTo(camera, duration, AnimationType.CUBIC, callback)
499}
500
501fun LocationWindow.panTo(point: Point, duration: Int = 1000) {
502 val currentCamera = camera
503 val newCamera = Camera(point, currentCamera.zoom, currentCamera.rotation)
504 val callback = CameraCallbackImpl()
505 moveTo(newCamera, duration, AnimationType.LINEAR, callback)
506}
507
508fun LocationWindow.rotateTo(rotation: Double, duration: Int = 1000) {
509 val currentCamera = camera
510 val newCamera = Camera(currentCamera.point, currentCamera.zoom, rotation)
511 val callback = CameraCallbackImpl()
512 moveTo(newCamera, duration, AnimationType.SINE, callback)
513}
514
515/**
516 * Usage example
517 */
518fun main() {
519 // Create example instance
520 val example = LocationWindowCameraExample()
521
522 // Assume we have LocationWindow
523 // val locationWindow: LocationWindow = getLocationWindow()
524 // example.locationWindow = locationWindow
525
526 // Demonstrate methods
527 println("=== LocationWindowCamera Examples ===")
528
529 // Cleanup
530 example.cleanup()
531
532 println("Examples ready to use!")
533}