Loading...
Searching...
No Matches
AsyncRouteManagerExample.m
Go to the documentation of this file.
1#import <Foundation/Foundation.h>
2#import <NavigineSDK/NavigineSDK.h>
3
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;
19- (void)demonstrateRoutePathUsage:(NCRoutePath *)routePath;
20- (void)demonstrateRouteNodeUsage:(NCRouteNode *)node;
21- (void)demonstrateRouteEventUsage:(NCRouteEvent *)event;
24- (void)demonstrateTurnEventUsage:(NCTurnEvent *)event;
25- (void)demonstrateTransitionEntryEventUsage:(NCTransitionEntryEvent *)event;
26- (void)demonstrateTransitionExitEventUsage:(NCTransitionExitEvent *)event;
27- (void)demonstrateTargetReachedEventUsage:(NCTargetReachedEvent *)event;
28- (void)demonstrateLocationPointUsage:(NCLocationPoint *)locationPoint;
29- (void)demonstratePointUsage:(NCPoint *)point;
31- (void)demonstrateAdvancedAsyncRoutingFeatures;
33- (void)cleanup;
34- (void)runExample;
35
36@end
37
38@implementation AsyncRouteManagerExample
39
40- (instancetype)init {
41 self = [super init];
42 if (self) {
43 [self initializeSdk];
45 }
46 return self;
47}
48
50 @try {
51 // [objc_NavigineSdk_getInstance]
52 // Get SDK instance
53 self.sdk = [NCNavigineSdk getInstance];
54 // [objc_NavigineSdk_getInstance]
55
56 // [objc_NavigineSdk_setUserHash]
57 // Set user hash
58 [self.sdk setUserHash:@"USER-HASH-HERE"];
59 // [objc_NavigineSdk_setUserHash]
60
61 // [objc_NavigineSdk_setServer]
62 // Set server URL (optional)
63 [self.sdk setServer:@"https://custom.navigine.com"];
64 // [objc_NavigineSdk_setServer]
65
66 // [objc_NavigineSdk_getLocationManager]
67 // Get LocationManager for working with locations
68 self.locationManager = [self.sdk getLocationManager];
69 // [objc_NavigineSdk_getLocationManager]
70
71 // [objc_NavigineSdk_getNavigationManager]
72 // Get NavigationManager for working with navigation
73 self.navigationManager = [self.sdk getNavigationManager:self.locationManager];
74 // [objc_NavigineSdk_getNavigationManager]
75
76 // [objc_NavigineSdk_getAsyncRouteManager]
77 // Get AsyncRouteManager for working with async routes
78 self.asyncRouteManager = [self.sdk getAsyncRouteManager:self.locationManager navigationManager:self.navigationManager];
79 // [objc_NavigineSdk_getAsyncRouteManager]
80
81 if (self.locationManager != nil && self.navigationManager != nil && self.asyncRouteManager != nil) {
82 NSLog(@"LocationManager, NavigationManager and AsyncRouteManager successfully initialized");
83 }
84 } @catch (NSException *exception) {
85 NSLog(@"Error initializing SDK: %@", exception.reason);
86 }
87}
88
93 // AsyncRouteListener is implemented via protocol
94 // No additional setup needed for Objective-C
95}
96
101 if (self.asyncRouteManager == nil) {
102 NSLog(@"AsyncRouteManager not initialized");
103 return;
104 }
105
106 // Create test destination point
107 NCPoint *destinationPoint = [[NCPoint alloc] initWithX:100.0 y:100.0];
108 NCLocationPoint *destinationLocationPoint = [[NCLocationPoint alloc] initWithPoint:destinationPoint locationId:12345 sublocationId:1];
109
110 // Create route options
111 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0];
112
113 // [objc_AsyncRouteManager_createRouteSession]
114 // Create route session with default graph tag
115 NCRouteSession *session = [self.asyncRouteManager createRouteSession:destinationLocationPoint routeOptions:routeOptions];
116 NSLog(@"Created route session with default graph tag");
117 // [objc_AsyncRouteManager_createRouteSession]
118
119 // [objc_AsyncRouteManager_createRouteSessionWithTag]
120 // Create route session with specific graph tag
121 NCRouteSession *sessionWithTag = [self.asyncRouteManager createRouteSessionWithTag:destinationLocationPoint routeOptions:routeOptions tag:@"main"];
122 NSLog(@"Created route session with 'main' graph tag");
123 // [objc_AsyncRouteManager_createRouteSessionWithTag]
124
125 // Store session for later use
126 self.currentSession = session;
127
128 // Add listener to session
129 // [objc_RouteSession_addRouteListener]
130 // Add route listener to session
132 // [objc_RouteSession_addRouteListener]
133}
134
138- (void)demonstrateRoutePathUsage:(NCRoutePath *)routePath {
139 if (routePath == nil) {
140 NSLog(@"RoutePath is null");
141 return;
142 }
143
144 // [objc_RoutePath_getLength]
145 // Get route length
146 double length = [routePath getLength];
147 NSLog(@"Route length: %f meters", length);
148 // [objc_RoutePath_getLength]
149
150 // [objc_RoutePath_getWeight]
151 // Get total route weight
152 double weight = [routePath getWeight];
153 NSLog(@"Route weight: %f", weight);
154 // [objc_RoutePath_getWeight]
155
156 // [objc_RoutePath_getNodes]
157 // Get route nodes
158 NSArray<NCRouteNode *> *nodes = [routePath getNodes];
159 NSLog(@"Number of route nodes: %lu", (unsigned long)nodes.count);
160 for (int i = 0; i < nodes.count && i < 3; i++) {
161 [self demonstrateRouteNodeUsage:nodes[i]];
162 }
163 // [objc_RoutePath_getNodes]
164
165 // [objc_RoutePath_head]
166 // Get route head (first 10 meters)
167 NCRoutePath *head = [routePath head:10.0];
168 if (head != nil) {
169 NSLog(@"Route head length: %f meters", [head getLength]);
170 }
171 // [objc_RoutePath_head]
172
173 // [objc_RoutePath_tail]
174 // Get route tail (remaining after 10 meters)
175 NCRoutePath *tail = [routePath tail:10.0];
176 if (tail != nil) {
177 NSLog(@"Route tail length: %f meters", [tail getLength]);
178 }
179 // [objc_RoutePath_tail]
180}
181
182- (void)demonstrateRouteNodeUsage:(NCRouteNode *)node {
183 // [objc_RouteNode_getPoint]
184 NCLocationPoint *point = [node getPoint];
186 // [objc_RouteNode_getPoint]
187
188 // [objc_RouteNode_getWeight]
189 float weight = [node getWeight];
190 NSLog(@"Node weight: %f", weight);
191 // [objc_RouteNode_getWeight]
192
193 // [objc_RouteNode_getDistance]
194 float distance = [node getDistance];
195 NSLog(@"Node distance: %f meters", distance);
196 // [objc_RouteNode_getDistance]
197
198 // [objc_RouteNode_getEvents]
199 NSArray<NCRouteEvent *> *events = [node getEvents];
200 for (NCRouteEvent *event in events) {
201 [self demonstrateRouteEventUsage:event];
202 }
203 // [objc_RouteNode_getEvents]
204}
205
209- (void)demonstrateRouteEventUsage:(NCRouteEvent *)event {
210 if (event == nil) {
211 NSLog(@"RouteEvent is null");
212 return;
213 }
214
215 // [objc_RouteEvent_getType]
216 // Get event type
217 NCRouteEventType type = [event getType];
218 NSLog(@"Event type: %ld", (long)type);
219 // [objc_RouteEvent_getType]
220
221 // [objc_RouteEvent_getTurnEvent]
222 NCTurnEvent *turnEvent = [event getTurnEvent];
223 if (turnEvent != nil) {
224 [self demonstrateTurnEventUsage:turnEvent];
225 }
226 // [objc_RouteEvent_getTurnEvent]
227
228 // [objc_RouteEvent_getTransitionEntryEvent]
229 NCTransitionEntryEvent *entryEvent = [event getTransitionEntryEvent];
230 if (entryEvent != nil) {
231 [self demonstrateTransitionEntryEventUsage:entryEvent];
232 }
233 // [objc_RouteEvent_getTransitionEntryEvent]
234
235 // [objc_RouteEvent_getTransitionExitEvent]
236 NCTransitionExitEvent *exitEvent = [event getTransitionExitEvent];
237 if (exitEvent != nil) {
239 }
240 // [objc_RouteEvent_getTransitionExitEvent]
241
242 // [objc_RouteEvent_getTargetReachedEvent]
243 NCTargetReachedEvent *targetEvent = [event getTargetReachedEvent];
244 if (targetEvent != nil) {
245 [self demonstrateTargetReachedEventUsage:targetEvent];
246 }
247 // [objc_RouteEvent_getTargetReachedEvent]
248}
249
254 // [objc_RouteEventType_values]
255 // Get all route event type values
256 NSArray<NSNumber *> *types = @[
261 ];
262 NSLog(@"Available route event types:");
263 for (NSNumber *type in types) {
264 NSLog(@" - %ld", (long)[type integerValue]);
265 }
266 // [objc_RouteEventType_values]
267}
268
270 // [objc_TurnType_values]
271 NSArray<NSNumber *> *types = @[
279 ];
280 for (NSNumber *type in types) {
281 NSLog(@" - %ld", (long)[type integerValue]);
282 }
283 // [objc_TurnType_values]
284}
285
286- (void)demonstrateTurnEventUsage:(NCTurnEvent *)event {
287 // [objc_TurnEvent_getType]
288 NCTurnType type = [event getType];
289 NSLog(@"Turn type: %ld", (long)type);
290 // [objc_TurnEvent_getType]
291
292 // [objc_TurnEvent_getAngle]
293 int32_t angle = [event getAngle];
294 NSLog(@"Turn angle: %d", angle);
295 // [objc_TurnEvent_getAngle]
296}
297
298- (void)demonstrateTransitionEntryEventUsage:(NCTransitionEntryEvent *)event {
299 // [objc_TransitionEntryEvent_getFrom]
300 int64_t from = [event getFrom];
301 NSLog(@"Transition entry from: %lld", from);
302 // [objc_TransitionEntryEvent_getFrom]
303
304 // [objc_TransitionEntryEvent_getTo]
305 int64_t to = [event getTo];
306 NSLog(@"Transition entry to: %lld", to);
307 // [objc_TransitionEntryEvent_getTo]
308}
309
310- (void)demonstrateTransitionExitEventUsage:(NCTransitionExitEvent *)event {
311 // [objc_TransitionExitEvent_getFrom]
312 int64_t from = [event getFrom];
313 NSLog(@"Transition exit from: %lld", from);
314 // [objc_TransitionExitEvent_getFrom]
315
316 // [objc_TransitionExitEvent_getTo]
317 int64_t to = [event getTo];
318 NSLog(@"Transition exit to: %lld", to);
319 // [objc_TransitionExitEvent_getTo]
320}
321
322- (void)demonstrateTargetReachedEventUsage:(NCTargetReachedEvent *)event {
323 // [objc_TargetReachedEvent_getIndex]
324 int64_t index = [event getIndex];
325 NSLog(@"Reached target index: %lld", index);
326 // [objc_TargetReachedEvent_getIndex]
327
328 // [objc_TargetReachedEvent_getPoint]
329 NCLocationPoint *point = [event getPoint];
331 // [objc_TargetReachedEvent_getPoint]
332}
333
337- (void)demonstrateLocationPointUsage:(NCLocationPoint *)locationPoint {
338 if (locationPoint == nil) {
339 NSLog(@"LocationPoint is null");
340 return;
341 }
342
343 // [objc_LocationPoint_getPoint]
344 // Get point coordinates
345 NCPoint *point = [locationPoint getPoint];
346 NSLog(@"Location point: (%f, %f)", [point getX], [point getY]);
347 // [objc_LocationPoint_getPoint]
348
349 // [objc_LocationPoint_getLocationId]
350 // Get location ID
351 int locationId = [locationPoint getLocationId];
352 NSLog(@"Location ID: %d", locationId);
353 // [objc_LocationPoint_getLocationId]
354
355 // [objc_LocationPoint_getSublocationId]
356 // Get sublocation ID
357 int sublocationId = [locationPoint getSublocationId];
358 NSLog(@"Sublocation ID: %d", sublocationId);
359 // [objc_LocationPoint_getSublocationId]
360
361 // [objc_LocationPoint_constructor]
362 // Create new location point
363 NCPoint *newPoint = [[NCPoint alloc] initWithX:25.0 y:35.0];
364 NCLocationPoint *newLocationPoint = [[NCLocationPoint alloc] initWithPoint:newPoint locationId:12345 sublocationId:2];
365 NSLog(@"Created new location point: %f, %f", [[newLocationPoint getPoint] getX], [[newLocationPoint getPoint] getY]);
366 // [objc_LocationPoint_constructor]
367}
368
372- (void)demonstratePointUsage:(NCPoint *)point {
373 if (point == nil) {
374 NSLog(@"Point is null");
375 return;
376 }
377
378 // [objc_Point_getX]
379 // Get X coordinate
380 double x = [point getX];
381 NSLog(@"Point X: %f", x);
382 // [objc_Point_getX]
383
384 // [objc_Point_getY]
385 // Get Y coordinate
386 double y = [point getY];
387 NSLog(@"Point Y: %f", y);
388 // [objc_Point_getY]
389
390 // [objc_Point_constructor]
391 // Create new point
392 NCPoint *newPoint = [[NCPoint alloc] initWithX:15.0 y:25.0];
393 NSLog(@"Created new point: (%f, %f)", [newPoint getX], [newPoint getY]);
394 // [objc_Point_constructor]
395}
396
401 NSLog(@"=== Route Options ===");
402
403 // [objc_RouteOptions_constructor]
404 // Create route options with custom parameters
405 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:3.0 maxProjectionDistance:7.0 maxAdvance:2.5];
406 NSLog(@"Created route options with smoothRadius: %f, maxProjectionDistance: %f, maxAdvance: %f",
407 [routeOptions getSmoothRadius], [routeOptions getMaxProjectionDistance], [routeOptions getMaxAdvance]);
408 // [objc_RouteOptions_constructor]
409
410 // Demonstrate different route options configurations
411 NSArray<NCRouteOptions *> *optionsList = @[
412 [[NCRouteOptions alloc] initWithSmoothRadius:0.0 maxProjectionDistance:5.0 maxAdvance:2.0], // No smoothing
413 [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:10.0 maxAdvance:1.0], // High smoothing, high projection distance
414 [[NCRouteOptions alloc] initWithSmoothRadius:1.0 maxProjectionDistance:3.0 maxAdvance:3.0], // Low smoothing, low projection distance
415 ];
416
417 for (int i = 0; i < optionsList.count; i++) {
418 NCRouteOptions *options = optionsList[i];
419 NSLog(@"Options %d: smoothRadius=%f, maxProjectionDistance=%f, maxAdvance=%f",
420 i + 1, [options getSmoothRadius], [options getMaxProjectionDistance], [options getMaxAdvance]);
421 }
422}
423
427- (void)demonstrateAdvancedAsyncRoutingFeatures {
428 NSLog(@"=== Advanced Async Routing Features ===");
429
430 if (self.asyncRouteManager == nil) {
431 return;
432 }
433
434 // Create multiple destination points
435 NSArray<NCLocationPoint *> *destinations = @[
436 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:50.0 y:50.0] locationId:12345 sublocationId:1],
437 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:100.0 y:100.0] locationId:12345 sublocationId:1],
438 [[NCLocationPoint alloc] initWithPoint:[[NCPoint alloc] initWithX:150.0 y:150.0] locationId:12345 sublocationId:1]
439 ];
440
441 // Create different route options for each destination
442 NSArray<NCRouteOptions *> *optionsList = @[
443 [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0],
444 [[NCRouteOptions alloc] initWithSmoothRadius:3.0 maxProjectionDistance:7.0 maxAdvance:1.5],
445 [[NCRouteOptions alloc] initWithSmoothRadius:1.0 maxProjectionDistance:3.0 maxAdvance:2.5],
446 ];
447
448 // Create sessions for each destination
449 NSMutableArray<NCRouteSession *> *sessions = [NSMutableArray array];
450 for (int i = 0; i < destinations.count; i++) {
451 NCRouteSession *session = [self.asyncRouteManager createRouteSession:destinations[i] routeOptions:optionsList[i]];
452 [sessions addObject:session];
453 NSLog(@"Created session %d for destination: %f, %f",
454 i + 1, [destinations[i].getPoint getX], [destinations[i].getPoint getY]);
455
456 [session addRouteListener:self];
457
458 [NSThread sleepForTimeInterval:1.0];
459 }
460
461 // Wait for route updates
462 [NSThread sleepForTimeInterval:3.0];
463
464 // Cancel all sessions
465 for (int i = 0; i < sessions.count; i++) {
466 // [objc_AsyncRouteManager_cancelRouteSession]
467 // Cancel route session
468 [self.asyncRouteManager cancelRouteSession:sessions[i]];
469 NSLog(@"Cancelled session %d", i + 1);
470 // [objc_AsyncRouteManager_cancelRouteSession]
471
472 [NSThread sleepForTimeInterval:1.0];
473 }
474}
475
480 NSLog(@"=== RouteStatus Enum Usage ===");
481
482 // [objc_RouteStatus_values]
483 // Demonstrate all RouteStatus values
484 NSLog(@"Available RouteStatus values:");
485 NSArray *allStatuses = @[@(NCRouteStatusMissingGraph), @(NCRouteStatusMissingPosition), @(NCRouteStatusMissingRoute), @(NCRouteStatusMissingProjection), @(NCRouteStatusNewRoute)];
486 for (NSNumber *statusNumber in allStatuses) {
487 NCRouteStatus status = [statusNumber integerValue];
488 NSLog(@" - %@: %@", @(status), @(status));
489 }
490
491 // Demonstrate status checking
493 switch (testStatus) {
495 NSLog(@"Router is ready for navigation");
496 break;
498 NSLog(@"Router is missing the route graph");
499 break;
501 NSLog(@"Router is missing the current position");
502 break;
504 NSLog(@"Current position is off the route graph");
505 break;
507 NSLog(@"Router unable to find the route to the destination point");
508 break;
509 }
510 // [objc_RouteStatus_values]
511}
512
517 NSLog(@"=== Route Session Management ===");
518
519 if (self.asyncRouteManager == nil) {
520 return;
521 }
522
523 // Create test destination
524 NCPoint *destinationPoint = [[NCPoint alloc] initWithX:75.0 y:75.0];
525 NCLocationPoint *destinationLocationPoint = [[NCLocationPoint alloc] initWithPoint:destinationPoint locationId:12345 sublocationId:1];
526 NCRouteOptions *routeOptions = [[NCRouteOptions alloc] initWithSmoothRadius:2.0 maxProjectionDistance:5.0 maxAdvance:2.0];
527
528 // Create session with default tag
529 NCRouteSession *defaultSession = [self.asyncRouteManager createRouteSession:destinationLocationPoint routeOptions:routeOptions];
530 NSLog(@"Created session with default tag");
531
532 // Create session with specific tag
533 NCRouteSession *taggedSession = [self.asyncRouteManager createRouteSessionWithTag:destinationLocationPoint routeOptions:routeOptions tag:@"elevator"];
534 NSLog(@"Created session with 'elevator' tag");
535
536 // Add listeners to both sessions
539
540 // Simulate some time passing
541 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
542 // Remove listeners
543 // [objc_RouteSession_removeRouteListener1]
544 // Remove route listener from session
547 NSLog(@"Removed listeners from sessions");
548 // [objc_RouteSession_removeRouteListener1]
549
550 // Cancel sessions
551 [self.asyncRouteManager cancelRouteSession:defaultSession];
552 [self.asyncRouteManager cancelRouteSession:taggedSession];
553 NSLog(@"Cancelled both sessions");
554 });
555}
556
560- (void)cleanup {
561 if (self.currentSession != nil) {
562 // [objc_RouteSession_removeRouteListener]
563 // Remove route listener
565 // [objc_RouteSession_removeRouteListener]
566 }
567
568 if (self.asyncRouteManager != nil && self.currentSession != nil) {
569 // [objc_AsyncRouteManager_cancelRouteSession_1]
570 // Cancel route session
571 [self.asyncRouteManager cancelRouteSession:self.currentSession];
572 // [objc_AsyncRouteManager_cancelRouteSession_1]
573 }
574}
575
579- (void)runExample {
580 NSLog(@"=== AsyncRouteManager Example ===");
581
582 [self demonstrateAsyncRouteManagerMethods];
583 [self demonstrateRouteEventTypes];
584 [self demonstrateTurnTypes];
585 [self demonstrateRouteOptions];
586 [self demonstrateRouteStatusUsage];
587 [self demonstrateRouteSessionManagement];
588 [self demonstrateAdvancedAsyncRoutingFeatures];
589
590 // Wait a bit for route updates
591 [NSThread sleepForTimeInterval:3.0];
592
593 [self cleanup];
594 NSLog(@"=== Example completed ===");
595}
596
597#pragma mark - NCAsyncRouteListener
598
599// [objc_AsyncRouteListener_onRouteChanged]
600- (void)onRouteChanged:(NCRouteStatus)status currentPath:(NCRoutePath *)currentPath {
601 NSLog(@"Route changed with status: %@", @(status));
602 if (status == NCRouteStatusNewRoute && currentPath != nil) {
603 [self demonstrateRoutePathUsage:currentPath];
604 } else {
605 NSLog(@"Route not ready, status: %@", @(status));
606 }
607}
608// [objc_AsyncRouteListener_onRouteChanged]
609
610// [objc_AsyncRouteListener_onRouteAdvanced]
611- (void)onRouteAdvanced:(float)distance point:(NCLocationPoint *)point {
612 NSLog(@"Route advanced: %f meters", distance);
613 [self demonstrateLocationPointUsage:point];
614}
615// [objc_AsyncRouteListener_onRouteAdvanced]
616
617@end
618
622int main(int argc, const char * argv[]) {
623 @autoreleasepool {
624 AsyncRouteManagerExample *example = [[AsyncRouteManagerExample alloc] init];
625 [example runExample];
626 }
627 return 0;
628}