車両の準備

このセクションでは、車両をトリップに対応させる方法について説明します。バックエンドで車両をトリップに一致させるには、次の各手順を完了する必要があります。

リスナーを設定する

Driver SDK を初期化して GMTDRidesharingDriverAPI インスタンスを作成したら、イベント リスナーを設定して、Fleet Engine とバックエンドに送信された車両の更新が成功したか失敗したかをモニタリングできます。これらのリスナーは、バックエンドとの通信が失敗した場合に運転手に通知するなど、運転手アプリ内でアクションをトリガーできます。

車両の更新イベントをリッスンする

運転手が運転手アプリで位置情報の更新を有効にすると、Driver SDK は GMTDVehicleReporter クラスを介して、Fleet Engine と顧客のバックエンドに定期的に車両の更新を送信します。GMTDVehicleReporterListener プロトコルを設定することで、アプリが更新イベントに応答するようにできます。

GMTDVehicleReporterListener を使用すると、次のイベントを処理できます。

  • vehicleReporter:didSucceedVehicleUpdate

    バックエンド サービスが車両の位置情報と状態の更新を正常に受信したことを運転手アプリに通知します。

  • vehicleReporter:didFailVehicleUpdate:withError

    車両の更新が失敗したことをリスナーに通知します。運転手が位置情報の更新を有効にしている限り、GMTDVehicleReporter クラスは最新のデータを 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.
    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

位置情報の更新を有効にする

位置情報の更新を有効にするには、運転手アプリの GMTDVehicleReporterlocationTrackingEnabledtrue に設定します。これにより、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

更新間隔を設定する

デフォルトでは、locationTrackingEnabledtrue に設定すると、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

次のステップ

トリップの詳細を設定する