Loading...
Searching...
No Matches
AsyncRouteManagerExample.m
Go to the documentation of this file.
1#import <Foundation/Foundation.h>
2#import <NavigineSDK/NavigineSDK.h>
3
8@interface AsyncRouteManagerExample : NSObject <NCAsyncRouteListener>
9
10@property (nonatomic, strong) NCNavigineSdk *sdk;
11@property (nonatomic, strong) NCLocationManager *locationManager;
12@property (nonatomic, strong) NCNavigationManager *navigationManager;
13@property (nonatomic, strong) NCAsyncRouteManager *asyncRouteManager;
14@property (nonatomic, strong) NCRouteSession *currentSession;
15
16- (void)initializeSdk;
18- (void)demonstrateAsyncRouteManagerMethods;
19- (void)demonstrateRoutePathUsage:(NCRoutePath *)routePath;
20- (void)demonstrateRouteEventUsage:(NCRouteEvent *)event;
21- (void)demonstrateRouteEventTypes;
22- (void)demonstrateLocationPointUsage:(NCLocationPoint *)locationPoint;
23- (void)demonstratePointUsage:(NCPoint *)point;
24- (void)demonstrateRouteOptions;
25- (void)demonstrateAdvancedAsyncRoutingFeatures;
26- (void)demonstrateRouteSessionManagement;
27- (void)cleanup;
28- (void)runExample;
29
30@end
31
32@implementation AsyncRouteManagerExample
33
34- (instancetype)init {
35 self = [super init];
36 if (self) {
37 [self initializeSdk];
39 }
40 return self;
41}
42
44 @try {
45 // [objc_NavigineSdk_getInstance]
46 // Get SDK instance
47 self.sdk = [NCNavigineSdk getInstance];
48 // [objc_NavigineSdk_getInstance]
49
50 // [objc_NavigineSdk_setUserHash]
51 // Set user hash
52 [self.sdk setUserHash:@"USER-HASH-HERE"];
53 // [objc_NavigineSdk_setUserHash]
54
55 // [objc_NavigineSdk_setServer]
56 // Set server URL (optional)
57 [self.sdk setServer:@"https://custom.navigine.com"];
58 // [objc_NavigineSdk_setServer]
59
60 // [objc_NavigineSdk_getLocationManager]
61 // Get LocationManager for working with locations
62 self.locationManager = [self.sdk getLocationManager];
63 // [objc_NavigineSdk_getLocationManager]
64
65 // [objc_NavigineSdk_getNavigationManager]
66 // Get NavigationManager for working with navigation
67 self.navigationManager = [self.sdk getNavigationManager:self.locationManager];
68 // [objc_NavigineSdk_getNavigationManager]
69
70 // [objc_NavigineSdk_getAsyncRouteManager]
71 // Get AsyncRouteManager for working with async routes
72 self.asyncRouteManager = [self.sdk getAsyncRouteManager:self.locationManager navigationManager:self.navigationManager];
73 // [objc_NavigineSdk_getAsyncRouteManager]
74
75 if (self.locationManager != nil && self.navigationManager != nil && self.asyncRouteManager != nil) {
76 NSLog(@"LocationManager, NavigationManager and AsyncRouteManager successfully initialized");
77 }
78 } @catch (NSException *exception) {
79 NSLog(@"Error initializing SDK: %@", exception.reason);
80 }
81}
82
87 // AsyncRouteListener is implemented via protocol
88 // No additional setup needed for Objective-C
89}
90
94- (void)demonstrateAsyncRouteManagerMethods {
95 if (self.asyncRouteManager == nil) {
96 NSLog(@"AsyncRouteManager not initialized");
97 return;
98 }
99
100 // Create test destination point
101 NCPoint *destinationPoint = [[NCPoint alloc] initWithX:100.0 y:100.0];
102 NCLocationPoint *destinationLocationPoint = [[NCLocationPoint alloc] initWithPoint:destinationPoint locationId:12345 sublocationId:1];
103
104 // Create route options
105 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0];
106
107 // [objc_AsyncRouteManager_createRouteSession]
108 // Create route session with default graph tag
109 NCRouteSession *session = [self.asyncRouteManager createRouteSession:destinationLocationPoint routeOptions:routeOptions];
110 NSLog(@"Created route session with default graph tag");
111 // [objc_AsyncRouteManager_createRouteSession]
112
113 // [objc_AsyncRouteManager_createRouteSessionWithTag]
114 // Create route session with specific graph tag
115 NCRouteSession *sessionWithTag = [self.asyncRouteManager createRouteSessionWithTag:destinationLocationPoint routeOptions:routeOptions tag:@"main"];
116 NSLog(@"Created route session with 'main' graph tag");
117 // [objc_AsyncRouteManager_createRouteSessionWithTag]
118
119 // Store session for later use
120 self.currentSession = session;
121
122 // Add listener to session
123 // [objc_RouteSession_addRouteListener]
124 // Add route listener to session
125 [session addRouteListener:self];
126 // [objc_RouteSession_addRouteListener]
127}
128
132- (void)demonstrateRoutePathUsage:(NCRoutePath *)routePath {
133 if (routePath == nil) {
134 NSLog(@"RoutePath is null");
135 return;
136 }
137
138 // [objc_RoutePath_getLength]
139 // Get route length
140 double length = [routePath getLength];
141 NSLog(@"Route length: %f meters", length);
142 // [objc_RoutePath_getLength]
143
144 // [objc_RoutePath_getPoints]
145 // Get route points
146 NSArray<NCLocationPoint *> *points = [routePath getPoints];
147 NSLog(@"Number of route points: %lu", (unsigned long)points.count);
148 for (int i = 0; i < points.count && i < 3; i++) {
149 NSLog(@"Point %d: %@", i + 1, points[i]);
150 }
151 // [objc_RoutePath_getPoints]
152
153 // [objc_RoutePath_getEvents]
154 // Get route events
155 NSArray<NCRouteEvent *> *events = [routePath getEvents];
156 NSLog(@"Number of route events: %lu", (unsigned long)events.count);
157 for (int i = 0; i < events.count && i < 3; i++) {
158 [self demonstrateRouteEventUsage:events[i]];
159 }
160 // [objc_RoutePath_getEvents]
161
162 // [objc_RoutePath_head]
163 // Get route head (first 10 meters)
164 NCRoutePath *head = [routePath head:10.0];
165 if (head != nil) {
166 NSLog(@"Route head length: %f meters", [head getLength]);
167 }
168 // [objc_RoutePath_head]
169
170 // [objc_RoutePath_tail]
171 // Get route tail (remaining after 10 meters)
172 NCRoutePath *tail = [routePath tail:10.0];
173 if (tail != nil) {
174 NSLog(@"Route tail length: %f meters", [tail getLength]);
175 }
176 // [objc_RoutePath_tail]
177}
178
182- (void)demonstrateRouteEventUsage:(NCRouteEvent *)event {
183 if (event == nil) {
184 NSLog(@"RouteEvent is null");
185 return;
186 }
187
188 // [objc_RouteEvent_getType]
189 // Get event type
190 NCRouteEventType type = [event getType];
191 NSLog(@"Event type: %ld", (long)type);
192 // [objc_RouteEvent_getType]
193
194 // [objc_RouteEvent_getValue]
195 // Get event value
196 int value = [event getValue];
197 NSLog(@"Event value: %d", value);
198 // [objc_RouteEvent_getValue]
199
200 // [objc_RouteEvent_getDistance]
201 // Get event distance
202 double distance = [event getDistance];
203 NSLog(@"Event distance: %f meters", distance);
204 // [objc_RouteEvent_getDistance]
205
206 // [objc_RouteEvent_constructor]
207 // Create new route event
208 NCRouteEvent *newEvent = [[NCRouteEvent alloc] initWithType:NCRouteEventTypeTurnLeft value:90 distance:50.0];
209 NSLog(@"Created new route event: %ld at %f meters", (long)[newEvent getType], [newEvent getDistance]);
210 // [objc_RouteEvent_constructor]
211}
212
216- (void)demonstrateRouteEventTypes {
217 // [objc_RouteEventType_values]
218 // Get all route event type values
219 NSArray<NSNumber *> *types = @[
220 @(NCRouteEventTypeTurnLeft),
221 @(NCRouteEventTypeTurnRight),
222 @(NCRouteEventTypeTransition)
223 ];
224 NSLog(@"Available route event types:");
225 for (NSNumber *type in types) {
226 NSLog(@" - %ld", (long)[type integerValue]);
227 }
228 // [objc_RouteEventType_values]
229}
230
234- (void)demonstrateLocationPointUsage:(NCLocationPoint *)locationPoint {
235 if (locationPoint == nil) {
236 NSLog(@"LocationPoint is null");
237 return;
238 }
239
240 // [objc_LocationPoint_getPoint]
241 // Get point coordinates
242 NCPoint *point = [locationPoint getPoint];
243 NSLog(@"Location point: (%f, %f)", [point getX], [point getY]);
244 // [objc_LocationPoint_getPoint]
245
246 // [objc_LocationPoint_getLocationId]
247 // Get location ID
248 int locationId = [locationPoint getLocationId];
249 NSLog(@"Location ID: %d", locationId);
250 // [objc_LocationPoint_getLocationId]
251
252 // [objc_LocationPoint_getSublocationId]
253 // Get sublocation ID
254 int sublocationId = [locationPoint getSublocationId];
255 NSLog(@"Sublocation ID: %d", sublocationId);
256 // [objc_LocationPoint_getSublocationId]
257
258 // [objc_LocationPoint_constructor]
259 // Create new location point
260 NCPoint *newPoint = [[NCPoint alloc] initWithX:25.0 y:35.0];
261 NCLocationPoint *newLocationPoint = [[NCLocationPoint alloc] initWithPoint:newPoint locationId:12345 sublocationId:2];
262 NSLog(@"Created new location point: %f, %f", [[newLocationPoint getPoint] getX], [[newLocationPoint getPoint] getY]);
263 // [objc_LocationPoint_constructor]
264}
265
269- (void)demonstratePointUsage:(NCPoint *)point {
270 if (point == nil) {
271 NSLog(@"Point is null");
272 return;
273 }
274
275 // [objc_Point_getX]
276 // Get X coordinate
277 double x = [point getX];
278 NSLog(@"Point X: %f", x);
279 // [objc_Point_getX]
280
281 // [objc_Point_getY]
282 // Get Y coordinate
283 double y = [point getY];
284 NSLog(@"Point Y: %f", y);
285 // [objc_Point_getY]
286
287 // [objc_Point_constructor]
288 // Create new point
289 NCPoint *newPoint = [[NCPoint alloc] initWithX:15.0 y:25.0];
290 NSLog(@"Created new point: (%f, %f)", [newPoint getX], [newPoint getY]);
291 // [objc_Point_constructor]
292}
293
297- (void)demonstrateRouteOptions {
298 NSLog(@"=== Route Options ===");
299
300 // [objc_RouteOptions_constructor]
301 // Create route options with custom parameters
302 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:3.0 maxProjectionDistance:7.0 maxAdvance:2.5];
303 NSLog(@"Created route options with smoothRadius: %f, maxProjectionDistance: %f, maxAdvance: %f",
304 [routeOptions getSmoothRadius], [routeOptions getMaxProjectionDistance], [routeOptions getMaxAdvance]);
305 // [objc_RouteOptions_constructor]
306
307 // Demonstrate different route options configurations
308 NSArray<NCRouteOptions *> *optionsList = @[
309 [[NCRouteOptions alloc] initWithSmoothRadius:0.0 maxProjectionDistance:5.0 maxAdvance:2.0], // No smoothing
310 [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:10.0 maxAdvance:1.0], // High smoothing, high projection distance
311 [[NCRouteOptions alloc] initWithSmoothRadius:1.0 maxProjectionDistance:3.0 maxAdvance:3.0], // Low smoothing, low projection distance
312 ];
313
314 for (int i = 0; i < optionsList.count; i++) {
315 NCRouteOptions *options = optionsList[i];
316 NSLog(@"Options %d: smoothRadius=%f, maxProjectionDistance=%f, maxAdvance=%f",
317 i + 1, [options getSmoothRadius], [options getMaxProjectionDistance], [options getMaxAdvance]);
318 }
319}
320
324- (void)demonstrateAdvancedAsyncRoutingFeatures {
325 NSLog(@"=== Advanced Async Routing Features ===");
326
327 if (self.asyncRouteManager == nil) {
328 return;
329 }
330
331 // Create multiple destination points
332 NSArray<NCLocationPoint *> *destinations = @[
333 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:50.0 y:50.0] locationId:12345 sublocationId:1],
334 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:100.0 y:100.0] locationId:12345 sublocationId:1],
335 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:150.0 y:150.0] locationId:12345 sublocationId:1]
336 ];
337
338 // Create different route options for each destination
339 NSArray<NCRouteOptions *> *optionsList = @[
340 [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0],
341 [[NCRouteOptions alloc] initWithSmoothRadius:3.0 maxProjectionDistance:7.0 maxAdvance:1.5],
342 [[NCRouteOptions alloc] initWithSmoothRadius:1.0 maxProjectionDistance:3.0 maxAdvance:2.5],
343 ];
344
345 // Create sessions for each destination
346 NSMutableArray<NCRouteSession *> *sessions = [NSMutableArray array];
347 for (int i = 0; i < destinations.count; i++) {
348 NCRouteSession *session = [self.asyncRouteManager createRouteSession:destinations[i] routeOptions:optionsList[i]];
349 [sessions addObject:session];
350 NSLog(@"Created session %d for destination: %f, %f",
351 i + 1, [destinations[i].getPoint getX], [destinations[i].getPoint getY]);
352
353 [session addRouteListener:self];
354
355 [NSThread sleepForTimeInterval:1.0];
356 }
357
358 // Wait for route updates
359 [NSThread sleepForTimeInterval:3.0];
360
361 // Cancel all sessions
362 for (int i = 0; i < sessions.count; i++) {
363 // [objc_AsyncRouteManager_cancelRouteSession]
364 // Cancel route session
365 [self.asyncRouteManager cancelRouteSession:sessions[i]];
366 NSLog(@"Cancelled session %d", i + 1);
367 // [objc_AsyncRouteManager_cancelRouteSession]
368
369 [NSThread sleepForTimeInterval:1.0];
370 }
371}
372
376- (void)demonstrateRouteStatusUsage {
377 NSLog(@"=== RouteStatus Enum Usage ===");
378
379 // [objc_RouteStatus_values]
380 // Demonstrate all RouteStatus values
381 NSLog(@"Available RouteStatus values:");
382 NSArray *allStatuses = @[@(NCRouteStatusMissingGraph), @(NCRouteStatusMissingPosition), @(NCRouteStatusMissingRoute), @(NCRouteStatusMissingProjection), @(NCRouteStatusNewRoute)];
383 for (NSNumber *statusNumber in allStatuses) {
384 NCRouteStatus status = [statusNumber integerValue];
385 NSLog(@" - %@: %@", @(status), @(status));
386 }
387
388 // Demonstrate status checking
389 NCRouteStatus testStatus = NCRouteStatusNewRoute;
390 switch (testStatus) {
391 case NCRouteStatusNewRoute:
392 NSLog(@"Router is ready for navigation");
393 break;
394 case NCRouteStatusMissingGraph:
395 NSLog(@"Router is missing the route graph");
396 break;
397 case NCRouteStatusMissingPosition:
398 NSLog(@"Router is missing the current position");
399 break;
400 case NCRouteStatusMissingProjection:
401 NSLog(@"Current position is off the route graph");
402 break;
403 case NCRouteStatusMissingRoute:
404 NSLog(@"Router unable to find the route to the destination point");
405 break;
406 }
407 // [objc_RouteStatus_values]
408}
409
413- (void)demonstrateRouteSessionManagement {
414 NSLog(@"=== Route Session Management ===");
415
416 if (self.asyncRouteManager == nil) {
417 return;
418 }
419
420 // Create test destination
421 NCPoint *destinationPoint = [[NCPoint alloc] initWithX:75.0 y:75.0];
422 NCLocationPoint *destinationLocationPoint = [[NCLocationPoint alloc] initWithPoint:destinationPoint locationId:12345 sublocationId:1];
423 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0];
424
425 // Create session with default tag
426 NCRouteSession *defaultSession = [self.asyncRouteManager createRouteSession:destinationLocationPoint routeOptions:routeOptions];
427 NSLog(@"Created session with default tag");
428
429 // Create session with specific tag
430 NCRouteSession *taggedSession = [self.asyncRouteManager createRouteSessionWithTag:destinationLocationPoint routeOptions:routeOptions tag:@"elevator"];
431 NSLog(@"Created session with 'elevator' tag");
432
433 // Add listeners to both sessions
434 [defaultSession addRouteListener:self];
435 [taggedSession addRouteListener:self];
436
437 // Simulate some time passing
438 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
439 // Remove listeners
440 // [objc_RouteSession_removeRouteListener]
441 // Remove route listener from session
442 [defaultSession removeRouteListener:self];
443 [taggedSession removeRouteListener:self];
444 NSLog(@"Removed listeners from sessions");
445 // [objc_RouteSession_removeRouteListener]
446
447 // Cancel sessions
448 [self.asyncRouteManager cancelRouteSession:defaultSession];
449 [self.asyncRouteManager cancelRouteSession:taggedSession];
450 NSLog(@"Cancelled both sessions");
451 });
452}
453
457- (void)cleanup {
458 if (self.currentSession != nil) {
459 // [objc_RouteSession_removeRouteListener]
460 // Remove route listener
461 [self.currentSession removeRouteListener:self];
462 // [objc_RouteSession_removeRouteListener]
463 }
464
465 if (self.asyncRouteManager != nil && self.currentSession != nil) {
466 // [objc_AsyncRouteManager_cancelRouteSession_1]
467 // Cancel route session
468 [self.asyncRouteManager cancelRouteSession:self.currentSession];
469 // [objc_AsyncRouteManager_cancelRouteSession_1]
470 }
471}
472
476- (void)runExample {
477 NSLog(@"=== AsyncRouteManager Example ===");
478
479 [self demonstrateAsyncRouteManagerMethods];
480 [self demonstrateRouteEventTypes];
481 [self demonstrateRouteOptions];
482 [self demonstrateRouteStatusUsage];
483 [self demonstrateRouteSessionManagement];
484 [self demonstrateAdvancedAsyncRoutingFeatures];
485
486 // Wait a bit for route updates
487 [NSThread sleepForTimeInterval:3.0];
488
489 [self cleanup];
490 NSLog(@"=== Example completed ===");
491}
492
493#pragma mark - NCAsyncRouteListener
494
495// [objc_AsyncRouteListener_onRouteChanged]
496- (void)onRouteChanged:(NCRouteStatus)status currentPath:(NCRoutePath *)currentPath {
497 NSLog(@"Route changed with status: %@", @(status));
498 if (status == NCRouteStatusNewRoute && currentPath != nil) {
499 [self demonstrateRoutePathUsage:currentPath];
500 } else {
501 NSLog(@"Route not ready, status: %@", @(status));
502 }
503}
504// [objc_AsyncRouteListener_onRouteChanged]
505
506// [objc_AsyncRouteListener_onRouteAdvanced]
507- (void)onRouteAdvanced:(float)distance point:(NCLocationPoint *)point {
508 NSLog(@"Route advanced: %f meters", distance);
509 [self demonstrateLocationPointUsage:point];
510}
511// [objc_AsyncRouteListener_onRouteAdvanced]
512
513@end
514
518int main(int argc, const char * argv[]) {
519 @autoreleasepool {
520 AsyncRouteManagerExample *example = [[AsyncRouteManagerExample alloc] init];
521 [example runExample];
522 }
523 return 0;
524}