准备好车辆

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

设置监听器

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

监听车辆更新事件

当司机在司机应用中启用位置信息更新时,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 类向 Driver 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

启用位置信息更新

若要启用位置信息更新,请将已开启的 locationTrackingEnabled 设为 true GMTDVehicleReporter。然后是 GMTDVehicleReporter 类 自动将位置更新发送到 Fleet Engine。部署了 Fleet Engine 客户后端服务匹配并将车辆分配给行程, GMTDVehicleReporter 类会在发生以下情况时自动发送路线更新: GMSNavigator 处于导航模式,即通过 setDestinations

Driver SDK 会设置路线,以匹配驾驶员的当前导航路径。接收者 确保位置信息更新准确无误,请在 setDestinations 中设置匹配的航点 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.
    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 时,驱动程序 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

后续步骤

设置行程详细信息