准备好车辆

设置监听器

初始化 Driver SDK 并创建 GMTDDeliveryDriverAPI 实例后,您可以设置事件监听器,以监控发送到车队引擎和后端的车辆更新是否成功。这些监听器可以在您的驾驶员应用中触发操作,例如在与后端通信失败时通知驾驶员。

监听车辆更新事件

当驾驶员在驾驶员应用中启用位置信息更新时,Driver SDK 会通过 GMTDDeliveryVehicleReporter 类将常规车辆更新发送到 Fleet Engine 和客户后端。您可以通过设置 GMTDVehicleReporterListener 协议,让应用响应更新事件。

借助 GMTDVehicleReporterListener,您可以处理以下事件:

  • vehicleReporter:didSucceedVehicleUpdate

    告知 Driver 应用后端服务已成功接收车辆位置和状态更新。

  • vehicleReporter:didFailVehicleUpdate:withError

    告知监听器车辆更新失败。只要驱动程序启用了位置信息更新,GMTDDeliveryVehicleReporter 类就会继续向车队引擎发送最新数据。

以下示例展示了如何设置 GMTDVehicleReporterListener 来处理这些事件:

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

启用位置信息更新

如需启用位置信息更新,请在 GMTDDeliveryVehicleReporter 上的驾驶员应用中将 locationTrackingEnabled 设置为 YES。然后,GMTDDeliveryVehicleReporter 类会自动将位置信息更新发送到车队引擎。此外,当 GMSNavigator 处于导航模式(即通过 setDestinations 设置目的地)时,GMTDDeliveryVehicleReporter 类会自动发送路线和预计到达时间更新。

Driver SDK 会设置路线,以匹配驾驶员的当前导航路径。为确保位置信息更新准确无误,请在 -setDestinations:callback: 中将航点设置为与车队引擎中的目的地一致。

以下示例展示了如何启用位置信息更新:

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

(可选)设置更新间隔

默认情况下,当您将 locationTrackingEnabled 设置为 YES 时,Driver SDK 会以 10 秒的间隔向 Fleet Engine 发送位置更新。您可以使用 locationUpdateInterval 将更新间隔时间更改为最短 5 秒或最长 60 秒。提高更新频率可能会导致请求速度变慢和出现错误。