准备好车辆

设置监听器

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

监听车辆更新事件

当驾驶员在驾驶应用中启用位置信息更新时,驱动程序 SDK 会发送 通过 GMTDDeliveryVehicleReporter 类。您可以通过设置 GMTDVehicleReporterListener 协议,让应用响应更新事件。

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

  • vehicleReporter:didSucceedVehicleUpdate

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

  • vehicleReporter:didFailVehicleUpdate:withError

    通知监听器车辆更新失败。只要驾驶员有 启用位置信息更新,GMTDDeliveryVehicleReporter 类会继续 将最新数据发送到 Fleet Engine。

以下示例展示了如何设置 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 类会自动将位置信息更新发送到 Fleet Engine。此外,当 GMSNavigator 处于导航模式时, 当目的地是通过 setDestinations 设置时, GMTDDeliveryVehicleReporter 类会自动发送路线和预计到达时间更新。

驱动程序 SDK 会设置与驾驶员的当前导航路径相匹配的路线。接收者 确保精确的位置更新,请在以下位置设置航点: -setDestinations:callback:,以便与 Fleet Engine 中的目的地匹配。

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

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 秒。更频繁的更新可能会导致 从而降低请求和错误的速度