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