Simulating Routes

Waring

This guide only applies to those not using the drop in UI. If you are creating your own navigation UI and would like to simulate a route, follow this guide

Why?

By default, with Xcode there are a few ways to simulate locations:

  1. In the Simulator.app, you can select City Bicycle or Freeway Drive

  1. Provide a GPX file with a series of latitudes and longtiudes.

However, there are issues with both of these. One is tied down to a specific location while the other does not allow you to controll the heading or course.

How

To simulate a route, we are overriding the location manager’s locationManager(_:didUpdateLocations:) from a series of locations that are coming from a provided route. To do this, we need to expose the location manager on MGLMapView since it is private.

Steps

  1. Add these two files to your project:

MGLMapView+CustomAdditions.h:

#import <Mapbox/Mapbox.h>

@interface MGLMapView (CustomAdditions) <CLLocationManagerDelegate>

// FIXME: This will be removed once https://github.com/mapbox/mapbox-gl-native/issues/6867 is implemented
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;

@property (nonatomic, readonly) CLLocationManager *locationManager;

@end

MGLMapView+CustomAdditions.m:

#import "MGLMapView+CustomAdditions.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"
// Supressing compiler warning until https://github.com/mapbox/mapbox-gl-native/issues/6867 is implemented
@implementation MGLMapView (CustomAdditions)
#pragma clang diagnostic pop

@dynamic locationManager;

@end
  1. Accept popup about Create Bridging Header

  1. Confirm in Build Settings that Objective-C Bridging Header has the correct path to the bridging header file just added.

  1. Provide a route to the SimulatedLocationManager
let simulator = SimulatedLocationManager(route: route)
let routeController = RouteController(along: userRoute!, directions: directions, locationManager: simulator)