准备好车辆

设置监听器

初始化驱动程序 SDK 并创建 GMTDDeliveryDriverAPI 实例后,您可以设置事件监听器来监控发送到 Fleet Engine 和后端的车辆更新是成功还是失败。这些监听器可以触发驱动程序应用中的操作,例如在与后端通信失败时通知驱动程序。

监听车辆更新事件

当驾驶员在驾驶员应用中启用位置信息更新时,驱动程序 SDK 会通过 GMTDDeliveryVehicleReporter 类向 Fleet Engine 和客户后端发送常规车辆更新。您可以通过设置 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 时,驱动程序 SDK 会以 10 秒的时间间隔向 Fleet Engine 发送位置信息更新。您可以使用 locationUpdateInterval 将更新间隔时间更改为最短 5 秒或最长 60 秒。提高更新频率可能会导致请求速度变慢和出现错误。