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