Set up listeners
After initializing the Driver SDK and creating a GMTDDeliveryDriverAPI
instance, you can set up event listeners to monitor the success or failure of
vehicle updates sent to Fleet Engine and your backend. These listeners can
trigger actions within your driver app, such as notifying the driver if
communication with your backend fails.
Listen for vehicle update events
When the driver enables location updates in the driver app, the Driver SDK sends
regular vehicle updates to Fleet Engine and the customer backend through the
GMTDDeliveryVehicleReporter
class. You can have the app respond to update
events by setting up the GMTDVehicleReporterListener
protocol.
With GMTDVehicleReporterListener
, you can handle the following events:
vehicleReporter:didSucceedVehicleUpdate
Informs the Driver app that the backend services successfully received the vehicle location and state update.
vehicleReporter:didFailVehicleUpdate:withError
Informs the listener that a vehicle update failed. As long as the driver has location updates enabled, the
GMTDDeliveryVehicleReporter
class continues to send the latest data to Fleet Engine.
The following example shows how to set up GMTDVehicleReporterListener
to
handle these events:
Swift
import GoogleRidesharingDriver
private let providerID = "INSERT_YOUR_PROVIDER_ID"
class SampleViewController: UIViewController, GMTDVehicleReporterListener {
private let mapView: GMSMapView
override func viewDidLoad() {
// Assumes you have implemented the sample code up to this step.
deliveryDriverAPI.vehicleReporter.add(self)
}
func vehicleReporter(_ vehicleReporter: GMTDDeliveryVehicleReporter, didSucceed vehicleUpdate: GMTDVehicleUpdate) {
// Handle update succeeded.
}
func vehicleReporter(_ vehicleReporter: GMTDDeliveryVehicleReporter, didFail vehicleUpdate: GMTDVehicleUpdate, withError error: Error) {
// Handle update failed.
}
}
Objective-C
SampleViewController.h
@interface SampleViewController : UIViewController<GMTDVehicleReporterListener>
@end
SampleViewController.m
#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>
static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";
@implementation SampleViewController {
GMSMapView *_mapView;
}
- (void)viewDidLoad {
// ASSUMES YOU IMPLEMENTED HAVE THE SAMPLE CODE UP TO THIS STEP.
[delivervehicleReporter addListener:self];
}
- (void)vehicleReporter:(GMTDDeliveryVehicleReporter *)vehicleReporter didSucceedVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate {
// Handle update succeeded.
}
- (void)vehicleReporter:(GMTDDeliveryVehicleReporter *)vehicleReporter didFailVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate withError:(NSError *)error {
// Handle update failed.
}
@end
Enable location updates
To enable location updates, in the driver app on
GMTDDeliveryVehicleReporter
, set locationTrackingEnabled
to
YES
. Then the
GMTDDeliveryVehicleReporter
class automatically sends location updates to
Fleet Engine. Additionally, when the GMSNavigator
is in navigation mode, which
is when a destination is set through setDestinations
, the
GMTDDeliveryVehicleReporter
class automatically sends route and ETA updates.
The Driver SDK sets the route to match the driver's current navigation path. To
ensure accurate location updates, set the waypoint in
-setDestinations:callback:
to match the destination in Fleet Engine.
The following example shows how to enable location updates:
Swift
import GoogleRidesharingDriver
private let providerID = "INSERT_YOUR_PROVIDER_ID"
class SampleViewController: UIViewController, GMTDVehicleReporterListener {
private let mapView: GMSMapView
override func viewDidLoad() {
// Assumes you have implemented the sample code up to this step.
deliveryDriverAPI.vehicleReporter.locationTrackingEnabled = true
}
}
Objective-C
SampleViewController.m
#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>
static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";
@implementation SampleViewController {
GMSMapView *_mapView;
}
- (void)viewDidLoad {
// ASSUMES YOU HAVE IMPLEMENTED THE SAMPLE CODE UP TO THIS STEP.
deliveryDriverAPI.vehicleReporter.locationTrackingEnabled = YES;
}
@end
(Optional) Set the update interval
By default, when you set locationTrackingEnabled
to YES
, the Driver SDK
sends location updates to Fleet Engine at a 10-second interval. You can change
the update interval with locationUpdateInterval
to a minimum update interval
of 5 seconds or a maximum of 60 seconds. More frequent updates may result in
slower requests and errors.