SDK for iOS Developer's Guide

Transit Routing

Transit routes are routes calculated using NMACoreRouter, with NMATransportMode set to NMATransportModePublicTransport in NMARoutingMode. With the transit routing feature you can calculate transit routes by using known online timetable information.

You can add a variety of restrictions, such as Avoid Boats, by using transitRoutingOptions property. You can also limit the desired number of transit vehicle changes by using maximumChanges property.

When a transit route is found, NMACoreRouter returns the calculation result block and route result via calculateRouteWithStops:routingMode:completionBlock: method. An NMARoute contains one or more maneuvers represented by the NMAManeuver class. Each of these maneuvers is either a pedestrian or a public transit maneuver. If a maneuver is a public transit maneuver, information specific for transit can be accessed by downcasting the NMAManeuver to NMATransitManeuver. An NMATransitManeuver contains one or more NMATransitRouteElement objects. Each NMATransitRouteElement object contains the departure station, the arrival station, and travel time of that transit maneuver.
Note: Transit directions are only available in certain areas of the world.

The following is an example of a transit route using NMACoreRouter:

NMACoreRouter* coreRouter = [[NMACoreRouter alloc] init];

NMAGeoCoordinates* geoCoord1 =
  [[NMAGeoCoordinates alloc] initWithLatitude:49.1966286 longitude:-123.0053635];
NMAGeoCoordinates* geoCoord2 =
  [[NMAGeoCoordinates alloc] initWithLatitude:49.1947289 longitude:-123.1762924];

NMAWaypoint* waypoint1 = [[NMAWaypoint alloc] initWithGeoCoordinates:geoCoord1];
NMAWaypoint* waypoint2 = [[NMAWaypoint alloc] initWithGeoCoordinates:geoCoord2];

NSMutableArray* stops = [[NSMutableArray alloc] initWithCapacity:4];
[stops addObject:waypoint1];
[stops addObject:waypoint2];

NMARoutingMode* routingMode = [[NMARoutingMode alloc]
  initWithRoutingType:NMARoutingTypeFastest
  transportMode:NMATransportModePublicTransport
  routingOptions:0];

[coreRouter calculateRouteWithStops:stops routingMode:routingMode
  completionBlock:^(NMARouteResult *routeResult, NMARoutingError error) {

    // If the route was calculated successfully
    if (!error && routeResult && routeResult.routes.count > 0)
    {
      NMARoute* route = [routeResult.routes objectAtIndex:0];
      // Render the route on the map
      NMAMapRoute* mapRoute = [NMAMapRoute mapRouteWithRoute:route];
      [mapView addMapObject:mapRoute];

      // In order to see the entire route, we orientate the map view
      // accordingly
      [mapView setBoundingBox:route.boundingBox
          withAnimation:NMAMapAnimationLinear];
    }
    else if (error)
    {
      // Display a message indicating route calculation failure
    }
  }];

Before displaying transit routes, set the map scheme to include transit so that the NMAMapRoute shows the color of the transit lines. You should also use setBoundingBox:withAnimation: method to pan and zoom the view to display the entire route.

// sets the map scheme to include transit
[mapView setMapScheme:NMAMapSchemeNormalDayTransit];
// zoom to display the entire route
[mapView setBoundingBox:route.boundingBox withAnimation:NMAMapAnimationBow];