4 * NavigationManager usage example for Swift
5 * Demonstrates working with navigation, positioning, and location tracking
7class NavigationManagerExample: NSObject, NCPositionListener, NCLocationListener {
8 private var sdk: NCNavigineSdk?
9 private var locationManager: NCLocationManager?
10 private var navigationManager: NCNavigationManager?
18 * Initialize SDK and get managers
20 private func initializeSdk() {
22 // [swift_NavigineSdk_getInstance]
24 sdk = NCNavigineSdk.getInstance()
25 // [swift_NavigineSdk_getInstance]
27 // [swift_NavigineSdk_setUserHash]
29 sdk?.setUserHash("USER-HASH-HERE")
30 // [swift_NavigineSdk_setUserHash]
32 // [swift_NavigineSdk_setServer]
33 // Set server URL (optional)
34 sdk?.setServer("https://custom.navigine.com")
35 // [swift_NavigineSdk_setServer]
37 // [swift_NavigineSdk_getLocationManager]
38 // Get LocationManager for working with locations
39 locationManager = sdk?.getLocationManager()
40 // [swift_NavigineSdk_getLocationManager]
42 // [swift_NavigineSdk_getNavigationManager]
43 // Get NavigationManager for working with navigation
44 navigationManager = sdk?.getNavigationManager(locationManager)
45 // [swift_NavigineSdk_getNavigationManager]
47 if locationManager != nil && navigationManager != nil {
48 print("LocationManager and NavigationManager successfully initialized")
51 print("Error initializing SDK: \(error)")
56 * Demonstrate NavigationManager methods
58 func demonstrateNavigationManagerMethods() {
59 guard let manager = navigationManager else {
60 print("NavigationManager not initialized")
64 // [swift_NavigationManager_addPositionListener]
65 // Add position listener
66 manager.addPositionListener(self)
67 // [swift_NavigationManager_addPositionListener]
69 // [swift_NavigationManager_startLogRecording]
70 // Start log recording
71 manager.startLogRecording()
72 // [swift_NavigationManager_startLogRecording]
74 // [swift_NavigationManager_addCheckPoint]
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]
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]
87 // [swift_NavigationManager_stopLogRecording]
89 manager.stopLogRecording()
90 // [swift_NavigationManager_stopLogRecording]
94 * Demonstrate Position class methods
96 func demonstratePositionUsage(_ position: NCPosition) {
97 // [swift_Position_getPoint]
98 // Get global point (WGS84 coordinates)
99 if let globalPoint = position.getPoint() {
100 demonstrateGlobalPointUsage(globalPoint)
102 // [swift_Position_getPoint]
104 // [swift_Position_getAccuracy]
105 // Get position accuracy
106 let accuracy = position.getAccuracy()
107 print("Position accuracy: \(accuracy) meters")
108 // [swift_Position_getAccuracy]
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")
115 // [swift_Position_getHeading]
117 // [swift_Position_getHeadingAccuracy]
118 // Get heading accuracy
119 if let headingAccuracy = position.getHeadingAccuracy() {
120 print("Heading accuracy: \(headingAccuracy) radians")
122 // [swift_Position_getHeadingAccuracy]
124 // [swift_Position_getLocationPoint]
125 // Get location point (metrics coordinates)
126 if let locationPoint = position.getLocationPoint() {
127 demonstrateLocationPointUsage(locationPoint)
129 // [swift_Position_getLocationPoint]
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")
136 // [swift_Position_getLocationHeading]
140 * Demonstrate GlobalPoint class methods
142 func demonstrateGlobalPointUsage(_ globalPoint: NCGlobalPoint) {
143 // [swift_GlobalPoint_getLatitude]
145 let latitude = globalPoint.getLatitude()
146 print("Latitude: \(latitude)")
147 // [swift_GlobalPoint_getLatitude]
149 // [swift_GlobalPoint_getLongitude]
151 let longitude = globalPoint.getLongitude()
152 print("Longitude: \(longitude)")
153 // [swift_GlobalPoint_getLongitude]
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]
163 * Demonstrate LocationPoint class methods
165 func demonstrateLocationPointUsage(_ locationPoint: NCLocationPoint) {
166 // [swift_LocationPoint_getPoint]
167 // Get point coordinates
168 if let point = locationPoint.getPoint() {
169 demonstratePointUsage(point)
171 // [swift_LocationPoint_getPoint]
173 // [swift_LocationPoint_getLocationId]
175 let locationId = locationPoint.getLocationId()
176 print("Location ID: \(locationId)")
177 // [swift_LocationPoint_getLocationId]
179 // [swift_LocationPoint_getSublocationId]
180 // Get sublocation ID
181 let sublocationId = locationPoint.getSublocationId()
182 print("Sublocation ID: \(sublocationId)")
183 // [swift_LocationPoint_getSublocationId]
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]
194 * Demonstrate Point class methods
196 func demonstratePointUsage(_ point: NCPoint) {
197 // [swift_Point_getX]
200 print("Point X: \(x)")
201 // [swift_Point_getX]
203 // [swift_Point_getY]
206 print("Point Y: \(y)")
207 // [swift_Point_getY]
209 // [swift_Point_constructor]
211 let newPoint = NCPoint(x: x, y: y)
212 print("Created new Point: \(newPoint)")
213 // [swift_Point_constructor]
217 * Demonstrate LocationManager integration
219 func demonstrateLocationManagerIntegration() {
220 guard let manager = locationManager else {
221 print("LocationManager not initialized")
225 // [swift_LocationManager_addLocationListener]
226 // Add location listener for navigation integration
227 manager.addLocationListener(self)
228 // [swift_LocationManager_addLocationListener]
230 // [swift_LocationManager_setLocationId]
231 // Set location ID to load
232 manager.setLocationId(12345)
233 // [swift_LocationManager_setLocationId]
235 // [swift_LocationManager_getLocationId]
236 // Get current location ID
237 let currentLocationId = manager.getLocationId()
238 print("Current location ID: \(currentLocationId)")
239 // [swift_LocationManager_getLocationId]
241 // [swift_LocationManager_setLocationUpdateInterval]
242 // Set location update interval
243 manager.setLocationUpdateInterval(300) // 5 minutes
244 // [swift_LocationManager_setLocationUpdateInterval]
246 // [swift_LocationManager_commitChanges]
248 manager.commitChanges()
249 // [swift_LocationManager_commitChanges]
253 * Demonstrate advanced navigation features
255 func demonstrateAdvancedNavigationFeatures() {
256 print("=== Advanced Navigation Features ===")
258 guard let manager = navigationManager else {
262 // Create multiple check points for path tracking
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)
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)")
279 // Add location measurements with different providers
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
286 let providers = ["gps", "network", "fused"]
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])")
298 * Demonstrate position tracking simulation
300 func demonstratePositionTrackingSimulation() {
301 print("=== Position Tracking Simulation ===")
303 guard let manager = navigationManager else {
307 // [swift_NavigationManager_startLogRecording]
308 // Start log recording for position tracking
309 manager.startLogRecording()
310 // [swift_NavigationManager_startLogRecording]
312 // Simulate position updates
314 let lat = 55.7558 + (Double(i) * 0.001) // Move north
315 let lon = 37.6176 + (Double(i) * 0.001) // Move east
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]
323 print("Simulated position \(i + 1): \(globalPoint)")
325 Thread.sleep(forTimeInterval: 1.0) // Wait 1 second between updates
328 // [swift_NavigationManager_stopLogRecording]
329 // Stop log recording
330 manager.stopLogRecording()
331 // [swift_NavigationManager_stopLogRecording]
338 if let manager = navigationManager {
339 // [swift_NavigationManager_removePositionListener]
340 // Remove position listener
341 manager.removePositionListener(self)
342 // [swift_NavigationManager_removePositionListener]
347 * Main demonstration method
350 print("=== NavigationManager Example ===")
352 demonstrateNavigationManagerMethods()
353 demonstrateLocationManagerIntegration()
354 demonstrateAdvancedNavigationFeatures()
355 demonstratePositionTrackingSimulation()
357 // Wait a bit for position updates
358 Thread.sleep(forTimeInterval: 3.0)
361 print("=== Example completed ===")
365// MARK: - NCPositionListener
367extension NavigationManagerExample {
368 // [swift_PositionListener_onPositionUpdated]
369 func onPositionUpdated(_ position: NCPosition) {
370 print("Position updated successfully")
371 demonstratePositionUsage(position)
373 // [swift_PositionListener_onPositionUpdated]
375 // [swift_PositionListener_onPositionError]
376 func onPositionError(_ error: Error?) {
377 if let error = error {
378 print("Position error: \(error.localizedDescription)")
381 // [swift_PositionListener_onPositionError]
384// MARK: - NCLocationListener
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]
397 // [swift_LocationListener_onLocationLoaded]
399 // [swift_LocationListener_onLocationUploaded]
400 func onLocationUploaded(_ locationId: Int32) {
401 print("Location uploaded: \(locationId)")
403 // [swift_LocationListener_onLocationUploaded]
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)")
411 // [swift_LocationListener_onLocationFailed]
415 * Function to run the example
418 let example = NavigationManagerExample()