Loading...
Searching...
No Matches
NavigationManagerExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * NavigationManager usage example for Swift
5 * Demonstrates working with navigation, positioning, and location tracking
6 */
7class NavigationManagerExample: NSObject, NCPositionListener, NCLocationListener {
8 private var sdk: NCNavigineSdk?
9 private var locationManager: NCLocationManager?
10 private var navigationManager: NCNavigationManager?
11
12 override init() {
13 super.init()
14 initializeSdk()
15 }
16
17 /**
18 * Initialize SDK and get managers
19 */
20 private func initializeSdk() {
21 do {
22 // [swift_NavigineSdk_getInstance]
23 // Get SDK instance
24 sdk = NCNavigineSdk.getInstance()
25 // [swift_NavigineSdk_getInstance]
26
27 // [swift_NavigineSdk_setUserHash]
28 // Set user hash
29 sdk?.setUserHash("USER-HASH-HERE")
30 // [swift_NavigineSdk_setUserHash]
31
32 // [swift_NavigineSdk_setServer]
33 // Set server URL (optional)
34 sdk?.setServer("https://custom.navigine.com")
35 // [swift_NavigineSdk_setServer]
36
37 // [swift_NavigineSdk_getLocationManager]
38 // Get LocationManager for working with locations
39 locationManager = sdk?.getLocationManager()
40 // [swift_NavigineSdk_getLocationManager]
41
42 // [swift_NavigineSdk_getNavigationManager]
43 // Get NavigationManager for working with navigation
44 navigationManager = sdk?.getNavigationManager(locationManager)
45 // [swift_NavigineSdk_getNavigationManager]
46
47 if locationManager != nil && navigationManager != nil {
48 print("LocationManager and NavigationManager successfully initialized")
49 }
50 } catch {
51 print("Error initializing SDK: \‍(error)")
52 }
53 }
54
55 /**
56 * Demonstrate NavigationManager methods
57 */
58 func demonstrateNavigationManagerMethods() {
59 guard let manager = navigationManager else {
60 print("NavigationManager not initialized")
61 return
62 }
63
64 // [swift_NavigationManager_addPositionListener]
65 // Add position listener
66 manager.addPositionListener(self)
67 // [swift_NavigationManager_addPositionListener]
68
69 // [swift_NavigationManager_startLogRecording]
70 // Start log recording
71 manager.startLogRecording()
72 // [swift_NavigationManager_startLogRecording]
73
74 // [swift_NavigationManager_addCheckPoint]
75 // Add check point
76 let point = NCPoint(x: 10.0, y: 20.0)
77 let locationPoint = NCLocationPoint(point: point, locationId: 12345, sublocationId: 1)
78 manager.addCheckPoint(locationPoint)
79 // [swift_NavigationManager_addCheckPoint]
80
81 // [swift_NavigationManager_addLocationMeasurement]
82 // Add location measurement
83 let globalPoint = NCGlobalPoint(latitude: 55.7558, longitude: 37.6176) // Moscow coordinates
84 manager.addLocationMeasurement(globalPoint, accuracy: 5.0, provider: "gps")
85 // [swift_NavigationManager_addLocationMeasurement]
86
87 // [swift_NavigationManager_stopLogRecording]
88 // Stop log recording
89 manager.stopLogRecording()
90 // [swift_NavigationManager_stopLogRecording]
91 }
92
93 /**
94 * Demonstrate Position class methods
95 */
96 func demonstratePositionUsage(_ position: NCPosition) {
97 // [swift_Position_getPoint]
98 // Get global point (WGS84 coordinates)
99 if let globalPoint = position.getPoint() {
100 demonstrateGlobalPointUsage(globalPoint)
101 }
102 // [swift_Position_getPoint]
103
104 // [swift_Position_getAccuracy]
105 // Get position accuracy
106 let accuracy = position.getAccuracy()
107 print("Position accuracy: \‍(accuracy) meters")
108 // [swift_Position_getAccuracy]
109
110 // [swift_Position_getHeading]
111 // Get heading (angle of rotation about the -Z axis in radians)
112 if let heading = position.getHeading() {
113 print("Heading: \‍(heading) radians")
114 }
115 // [swift_Position_getHeading]
116
117 // [swift_Position_getHeadingAccuracy]
118 // Get heading accuracy
119 if let headingAccuracy = position.getHeadingAccuracy() {
120 print("Heading accuracy: \‍(headingAccuracy) radians")
121 }
122 // [swift_Position_getHeadingAccuracy]
123
124 // [swift_Position_getLocationPoint]
125 // Get location point (metrics coordinates)
126 if let locationPoint = position.getLocationPoint() {
127 demonstrateLocationPointUsage(locationPoint)
128 }
129 // [swift_Position_getLocationPoint]
130
131 // [swift_Position_getLocationHeading]
132 // Get location heading (with respect to sublocation north)
133 if let locationHeading = position.getLocationHeading() {
134 print("Location heading: \‍(locationHeading) radians")
135 }
136 // [swift_Position_getLocationHeading]
137 }
138
139 /**
140 * Demonstrate GlobalPoint class methods
141 */
142 func demonstrateGlobalPointUsage(_ globalPoint: NCGlobalPoint) {
143 // [swift_GlobalPoint_getLatitude]
144 // Get latitude
145 let latitude = globalPoint.getLatitude()
146 print("Latitude: \‍(latitude)")
147 // [swift_GlobalPoint_getLatitude]
148
149 // [swift_GlobalPoint_getLongitude]
150 // Get longitude
151 let longitude = globalPoint.getLongitude()
152 print("Longitude: \‍(longitude)")
153 // [swift_GlobalPoint_getLongitude]
154
155 // [swift_GlobalPoint_constructor]
156 // Create new GlobalPoint
157 let newPoint = NCGlobalPoint(latitude: latitude, longitude: longitude)
158 print("Created new GlobalPoint: \‍(newPoint)")
159 // [swift_GlobalPoint_constructor]
160 }
161
162 /**
163 * Demonstrate LocationPoint class methods
164 */
165 func demonstrateLocationPointUsage(_ locationPoint: NCLocationPoint) {
166 // [swift_LocationPoint_getPoint]
167 // Get point coordinates
168 if let point = locationPoint.getPoint() {
169 demonstratePointUsage(point)
170 }
171 // [swift_LocationPoint_getPoint]
172
173 // [swift_LocationPoint_getLocationId]
174 // Get location ID
175 let locationId = locationPoint.getLocationId()
176 print("Location ID: \‍(locationId)")
177 // [swift_LocationPoint_getLocationId]
178
179 // [swift_LocationPoint_getSublocationId]
180 // Get sublocation ID
181 let sublocationId = locationPoint.getSublocationId()
182 print("Sublocation ID: \‍(sublocationId)")
183 // [swift_LocationPoint_getSublocationId]
184
185 // [swift_LocationPoint_constructor]
186 // Create new LocationPoint
187 let newPoint = NCPoint(x: 15.0, y: 25.0)
188 let newLocationPoint = NCLocationPoint(point: newPoint, locationId: locationId, sublocationId: sublocationId)
189 print("Created new LocationPoint: \‍(newLocationPoint)")
190 // [swift_LocationPoint_constructor]
191 }
192
193 /**
194 * Demonstrate Point class methods
195 */
196 func demonstratePointUsage(_ point: NCPoint) {
197 // [swift_Point_getX]
198 // Get X coordinate
199 let x = point.getX()
200 print("Point X: \‍(x)")
201 // [swift_Point_getX]
202
203 // [swift_Point_getY]
204 // Get Y coordinate
205 let y = point.getY()
206 print("Point Y: \‍(y)")
207 // [swift_Point_getY]
208
209 // [swift_Point_constructor]
210 // Create new Point
211 let newPoint = NCPoint(x: x, y: y)
212 print("Created new Point: \‍(newPoint)")
213 // [swift_Point_constructor]
214 }
215
216 /**
217 * Demonstrate LocationManager integration
218 */
219 func demonstrateLocationManagerIntegration() {
220 guard let manager = locationManager else {
221 print("LocationManager not initialized")
222 return
223 }
224
225 // [swift_LocationManager_addLocationListener]
226 // Add location listener for navigation integration
227 manager.addLocationListener(self)
228 // [swift_LocationManager_addLocationListener]
229
230 // [swift_LocationManager_setLocationId]
231 // Set location ID to load
232 manager.setLocationId(12345)
233 // [swift_LocationManager_setLocationId]
234
235 // [swift_LocationManager_getLocationId]
236 // Get current location ID
237 let currentLocationId = manager.getLocationId()
238 print("Current location ID: \‍(currentLocationId)")
239 // [swift_LocationManager_getLocationId]
240
241 // [swift_LocationManager_setLocationUpdateInterval]
242 // Set location update interval
243 manager.setLocationUpdateInterval(300) // 5 minutes
244 // [swift_LocationManager_setLocationUpdateInterval]
245
246 // [swift_LocationManager_commitChanges]
247 // Commit changes
248 manager.commitChanges()
249 // [swift_LocationManager_commitChanges]
250 }
251
252 /**
253 * Demonstrate advanced navigation features
254 */
255 func demonstrateAdvancedNavigationFeatures() {
256 print("=== Advanced Navigation Features ===")
257
258 guard let manager = navigationManager else {
259 return
260 }
261
262 // Create multiple check points for path tracking
263 let checkPoints = [
264 NCPoint(x: 0.0, y: 0.0),
265 NCPoint(x: 10.0, y: 10.0),
266 NCPoint(x: 20.0, y: 20.0),
267 NCPoint(x: 30.0, y: 30.0)
268 ]
269
270 for (index, point) in checkPoints.enumerated() {
271 let locationPoint = NCLocationPoint(point: point, locationId: 12345, sublocationId: 1)
272 // [swift_NavigationManager_addCheckPoint]
273 // Add check point for path tracking
274 manager.addCheckPoint(locationPoint)
275 // [swift_NavigationManager_addCheckPoint]
276 print("Added check point \‍(index + 1): \‍(locationPoint)")
277 }
278
279 // Add location measurements with different providers
280 let measurements = [
281 NCGlobalPoint(latitude: 55.7558, longitude: 37.6176), // Moscow
282 NCGlobalPoint(latitude: 40.7128, longitude: -74.0060), // New York
283 NCGlobalPoint(latitude: 51.5074, longitude: -0.1278) // London
284 ]
285
286 let providers = ["gps", "network", "fused"]
287
288 for (index, globalPoint) in measurements.enumerated() {
289 // [swift_NavigationManager_addLocationMeasurement]
290 // Add location measurement with different providers
291 manager.addLocationMeasurement(globalPoint, accuracy: 3.0 + Double(index), provider: providers[index % providers.count])
292 // [swift_NavigationManager_addLocationMeasurement]
293 print("Added measurement \‍(index + 1) with provider: \‍(providers[index % providers.count])")
294 }
295 }
296
297 /**
298 * Demonstrate position tracking simulation
299 */
300 func demonstratePositionTrackingSimulation() {
301 print("=== Position Tracking Simulation ===")
302
303 guard let manager = navigationManager else {
304 return
305 }
306
307 // [swift_NavigationManager_startLogRecording]
308 // Start log recording for position tracking
309 manager.startLogRecording()
310 // [swift_NavigationManager_startLogRecording]
311
312 // Simulate position updates
313 for i in 0..<5 {
314 let lat = 55.7558 + (Double(i) * 0.001) // Move north
315 let lon = 37.6176 + (Double(i) * 0.001) // Move east
316
317 let globalPoint = NCGlobalPoint(latitude: lat, longitude: lon)
318 // [swift_NavigationManager_addLocationMeasurement]
319 // Add simulated location measurement
320 manager.addLocationMeasurement(globalPoint, accuracy: 2.0, provider: "simulated")
321 // [swift_NavigationManager_addLocationMeasurement]
322
323 print("Simulated position \‍(i + 1): \‍(globalPoint)")
324
325 Thread.sleep(forTimeInterval: 1.0) // Wait 1 second between updates
326 }
327
328 // [swift_NavigationManager_stopLogRecording]
329 // Stop log recording
330 manager.stopLogRecording()
331 // [swift_NavigationManager_stopLogRecording]
332 }
333
334 /**
335 * Clean up resources
336 */
337 func cleanup() {
338 if let manager = navigationManager {
339 // [swift_NavigationManager_removePositionListener]
340 // Remove position listener
341 manager.removePositionListener(self)
342 // [swift_NavigationManager_removePositionListener]
343 }
344 }
345
346 /**
347 * Main demonstration method
348 */
349 func runExample() {
350 print("=== NavigationManager Example ===")
351
352 demonstrateNavigationManagerMethods()
353 demonstrateLocationManagerIntegration()
354 demonstrateAdvancedNavigationFeatures()
355 demonstratePositionTrackingSimulation()
356
357 // Wait a bit for position updates
358 Thread.sleep(forTimeInterval: 3.0)
359
360 cleanup()
361 print("=== Example completed ===")
362 }
363}
364
365// MARK: - NCPositionListener
366
367extension NavigationManagerExample {
368 // [swift_PositionListener_onPositionUpdated]
369 func onPositionUpdated(_ position: NCPosition) {
370 print("Position updated successfully")
371 demonstratePositionUsage(position)
372 }
373 // [swift_PositionListener_onPositionUpdated]
374
375 // [swift_PositionListener_onPositionError]
376 func onPositionError(_ error: Error?) {
377 if let error = error {
378 print("Position error: \‍(error.localizedDescription)")
379 }
380 }
381 // [swift_PositionListener_onPositionError]
382}
383
384// MARK: - NCLocationListener
385
386extension NavigationManagerExample {
387 // [swift_LocationListener_onLocationLoaded]
388 func onLocationLoaded(_ location: NCLocation?) {
389 print("Location loaded for navigation")
390 if let location = location {
391 // [swift_LocationManager_setLocationId]
392 // Set location ID for navigation
393 locationManager?.setLocationId(location.getId())
394 // [swift_LocationManager_setLocationId]
395 }
396 }
397 // [swift_LocationListener_onLocationLoaded]
398
399 // [swift_LocationListener_onLocationUploaded]
400 func onLocationUploaded(_ locationId: Int32) {
401 print("Location uploaded: \‍(locationId)")
402 }
403 // [swift_LocationListener_onLocationUploaded]
404
405 // [swift_LocationListener_onLocationFailed]
406 func onLocationFailed(_ locationId: Int32, error: Error?) {
407 if let error = error {
408 print("Failed to load location \‍(locationId): \‍(error.localizedDescription)")
409 }
410 }
411 // [swift_LocationListener_onLocationFailed]
412}
413
414/**
415 * Function to run the example
416 */
417func main() {
418 let example = NavigationManagerExample()
419 example.runExample()
420}
421
422// Run the example
423main()