請上車

本節說明如何準備車輛,以供行程使用。您必須完成下列各項步驟,後端才能將車輛與行程配對。

設定監聽器

初始化 Driver SDK 並建立 GMTDRidesharingDriverAPI 執行個體後,您可以設定事件監聽器,監控傳送至 Fleet Engine 和後端的車輛更新是否成功。這些事件監聽器可觸發司機應用程式中的動作,例如在與後端通訊失敗時通知司機。

監聽車輛更新事件

司機在司機應用程式中啟用位置更新功能後,司機 SDK 會透過 GMTDVehicleReporter 類別,定期將車輛更新資訊傳送至 Fleet Engine 和客戶後端。您可以設定 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 類別會自動將位置資訊更新傳送至 Fleet Engine。GMTDVehicleReporter當 Fleet Engine 和客戶後端服務比對並指派車輛給行程後,如果 GMSNavigator 處於導航模式 (即透過 setDestinations 設定目的地),GMTDVehicleReporter 類別會自動傳送路線更新。

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,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

後續步驟

設定行程詳細資料