准备好车辆

本部分介绍了如何让车辆做好行程准备。您必须先完成以下每个步骤,然后后端才能将车辆与行程进行匹配。

设置监听器

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

监听车辆更新事件

当司机在司机应用中启用位置信息更新时,Driver SDK 会通过 GMTDVehicleReporter 类将定期车辆更新发送到车队引擎和客户后端。您可以通过设置 GMTDVehicleReporterListener 协议,让应用响应更新事件。

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

以下示例展示了如何设置 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.
    ridesharingDriverAPI.vehicleReporter.add(self)
  }

  func vehicleReporter(_ vehicleReporter: GMTDVehicleReporter, didSucceed vehicleUpdate: GMTDVehicleUpdate) {
    // Handle update succeeded.
  }

  func vehicleReporter(_ vehicleReporter: GMTDVehicleReporter, 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 have implemented the sample code up to this step.
  [ridesharingDriverAPI.vehicleReporter addListener:self];
}

- (void)vehicleReporter:(GMTDVehicleReporter *)vehicleReporter didSucceedVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate {
  // Handle update succeeded.
}

- (void)vehicleReporter:(GMTDVehicleReporter *)vehicleReporter didFailVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate withError:(NSError *)error {
  // Handle update failed.
}

@end

监听车辆位置信息更新

Navigation SDK 通过 GMSRoadSnappedLocationProvider 类为驱动程序 SDK 提供位置信息更新。如需接收这些更新,您必须将 GMTDVehicleReporter 设置为监听器。

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.
    if let roadSnappedLocationProvider = mapView.roadSnappedLocationProvider {
      roadSnappedLocationProvider.add(ridesharingDriverAPI.vehicleReporter)
      roadSnappedLocationProvider.startUpdatingLocation()
    }
  }
}

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 have implemented the sample code up to this step.
  [_mapView.roadSnappedLocationProvider addListener:ridesharingDriverAPI.vehicleReporter];
  [_mapView.roadSnappedLocationProvider startUpdatingLocation];
}

@end

启用位置信息更新

如需启用位置信息更新,请在驾驶员应用的 GMTDVehicleReporter 上将 locationTrackingEnabled 设为 true。然后,GMTDVehicleReporter 类会自动将位置信息更新发送到车队引擎。在车队引擎和客户后端服务匹配并将车辆分配给行程后,当 GMSNavigator 处于导航模式(即通过 setDestinations 设置目的地)时,GMTDVehicleReporter 类会自动发送路线更新。

驱动程序 SDK 会设置与驾驶员的当前导航路径匹配的路线。为确保位置信息准确更新,请在 setDestinations 中将航点设置为与车队引擎中的目的地一致。

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

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.
    ridesharingDriverAPI.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.
  ridesharingDriverAPI.vehicleReporter.locationTrackingEnabled = YES;
}

@end

设置更新间隔

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

将车辆状态设为在线

启用位置信息更新后,将车辆状态设置为 ONLINE,使车辆可供 Fleet Engine 中的搜索查询显示。

以下示例展示了如何将车辆状态设置为 ONLINE。如需了解详情,请参阅 updateVehicleState

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.
    ridesharingDriverAPI.vehicleReporter.update(.online)
  }
}

Objective-C

#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.
  [ridesharingDriverAPI.vehicleReporter
                                   updateVehicleState:GMTDVehicleStateOnline];
}

@end

后续步骤

设置行程详细信息