Navigation with Waypoints

This is the same idea as our earlier navigation example , but this one demonstrates how to navigate a route with many waypoints (rather than just a single origin and destination).

If you're testing at your desk, you'll probably want to simulate your location, as before. However, skip this line if you want to test in the real world.

TGDrivingSimulator.shared.startingCoordinate = CLLocationCoordinate2D(latitude: 34.101558, longitude: -118.340944) // Grauman's Chinese Theatre
TGDrivingSimulator.shared.enabled = true

Now we will configure the turn-by-turn navigation. This is much the same as before, except instead of an origin and destination, we pass an array of waypoints. Note that you can set a custom description when creating a waypoint; if you do this, that's the string that will be shown in the UI to represent this waypoint. Otherwise, you can just set it to nil and the SDK will find the street address for the coordinates (via reverse geocoding).

// Get these coordinates from your app, these are just a sample
let waypoints: [TGWaypoint] = [
    TGWaypoint(coordinate: CLLocationCoordinate2D(latitude: 34.101558, longitude: -118.340944), address: nil, description: "Grauman's Chinese Theatre"),
    TGWaypoint(coordinate: CLLocationCoordinate2D(latitude: 34.07902875, longitude: -118.379441), address: nil, description: "Quarter point"),
    TGWaypoint(coordinate: CLLocationCoordinate2D(latitude: 34.0564995, longitude: -118.417938), address: nil, description: "Midpoint"),
    TGWaypoint(coordinate: CLLocationCoordinate2D(latitude: 34.03397025, longitude: -118.456435), address: nil, description: "Three-quarters point"),
    TGWaypoint(coordinate: CLLocationCoordinate2D(latitude: 34.011441, longitude: -118.494932), address: nil, description: "Santa Monica Pier"),
]

// Configure turn-by-turn navigation
let config = TGTurnByTurnConfiguration()
config.showsOriginIcon = false
config.commencementSpeech = "Let's go!"
config.proceedToRouteSpeech = "Please proceed to the route."
config.arrivalSpeech = "You have arrived."

config.routeRequest = TGRouteRequest(waypoints: waypoints)

// Display it
let viewController = TGPreviewViewController.create(with: config).embedInNavigationController()
present(viewController, animated: true, completion: nil)

This will show you a preview of all the waypoints, from which you can then begin turn-by-turn navigation.

If you're testing at your desk, you'll probably want to simulate your location, as before. However, skip this line if you want to test in the real world.

TGDrivingSimulator.sharedDrivingSimulator.startingCoordinate = CLLocationCoordinate2DMake(34.101558, -118.340944); // Grauman's Chinese Theatre
TGDrivingSimulator.sharedDrivingSimulator.enabled = YES;

Now we will configure the turn-by-turn navigation. This is much the same as before, except instead of an origin and destination, we pass an array of waypoints. Note that you can set a custom description when creating a waypoint; if you do this, that's the string that will be shown in the UI to represent this waypoint. Otherwise, you can just set it to nil and the SDK will find the street address for the coordinates (via reverse geocoding).

// Get these coordinates from your app, these are just a sample
NSArray<TGWaypoint *> *waypoints = @[
    [TGWaypoint.alloc initWithCoordinate:CLLocationCoordinate2DMake(34.101558, -118.340944) address:nil description:@"Grauman's Chinese Theatre"],
    [TGWaypoint.alloc initWithCoordinate:CLLocationCoordinate2DMake(34.07902875, -118.379441) address:nil description:@"Quarter point"],
    [TGWaypoint.alloc initWithCoordinate:CLLocationCoordinate2DMake(34.0564995, -118.417938) address:nil description:@"Midpoint"],
    [TGWaypoint.alloc initWithCoordinate:CLLocationCoordinate2DMake(34.03397025, -118.456435) address:nil description:@"Three-quarters point"],
    [TGWaypoint.alloc initWithCoordinate:CLLocationCoordinate2DMake(34.011441, -118.494932) address:nil description:@"Santa Monica Pier"],
];

// Configure turn-by-turn navigation
TGTurnByTurnConfiguration *config = [TGTurnByTurnConfiguration new];
config.showsOriginIcon = NO;
config.commencementSpeech = @"Let's go!";
config.proceedToRouteSpeech = @"Please proceed to the route.";
config.arrivalSpeech = @"You have arrived.";

config.routeRequest = [TGRouteRequest.alloc initWithWaypoints:waypoints];

// Display it
UINavigationController *viewController = [[TGPreviewViewController createWithConfiguration:config] embedInNavigationController];
[self presentViewController:viewController animated:YES completion:nil];

This will show you a preview of all the waypoints, from which you can then begin turn-by-turn navigation.

Check out the iOS Reference App to see this example in action.

Look for example 208 in the app.