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