Chuẩn bị xe sẵn sàng

Phần này cho biết cách chuẩn bị xe cho chuyến đi. Bạn phải hoàn tất từng bước sau đây trước khi phần phụ trợ của bạn có thể so khớp một chiếc xe với một chuyến đi.

Thiết lập trình nghe

Sau khi khởi chạy SDK trình điều khiển và tạo một GMTDRidesharingDriverAPI Bạn có thể thiết lập trình nghe sự kiện để theo dõi trạng thái thành công hay không thành công của thông tin cập nhật về xe được gửi đến Fleet Engine và phần phụ trợ của bạn. Những trình nghe này có thể kích hoạt các hành động trong ứng dụng trình điều khiển, chẳng hạn như thông báo cho người lái xe nếu không kết nối được với phần phụ trợ.

Theo dõi các sự kiện cập nhật xe

Khi người lái xe bật tính năng cập nhật vị trí trong ứng dụng trình điều khiển, SDK trình điều khiển gửi thông tin cập nhật thường xuyên về xe đến Fleet Engine và khách hàng phần phụ trợ thông qua lớp GMTDVehicleReporter. Bạn có thể yêu cầu ứng dụng phản hồi để cập nhật sự kiện bằng cách thiết lập giao thức GMTDVehicleReporterListener.

Với GMTDVehicleReporterListener, bạn có thể xử lý các sự kiện sau:

  • vehicleReporter:didSucceedVehicleUpdate

    Thông báo cho ứng dụng Driver rằng các dịch vụ phụ trợ đã nhận được thông tin cập nhật về trạng thái và vị trí của xe.

  • vehicleReporter:didFailVehicleUpdate:withError

    Thông báo cho người nghe biết rằng không cập nhật được xe. Miễn là người lái xe Đã bật tính năng cập nhật vị trí, lớp GMTDVehicleReporter sẽ tiếp tục gửi dữ liệu mới nhất lên Fleet Engine.

Các ví dụ sau đây minh hoạ cách thiết lập GMTDVehicleReporterListener để xử lý các sự kiện sau:

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

Theo dõi thông tin cập nhật về vị trí của xe

SDK điều hướng cung cấp thông tin cập nhật vị trí cho SDK trình điều khiển thông qua Lớp GMSRoadSnappedLocationProvider. Để nhận những nội dung cập nhật đó, bạn phải đặt GMTDVehicleReporter làm thính giả.

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

Bật tính năng cập nhật vị trí

Để bật tính năng cập nhật vị trí, hãy bật locationTrackingEnabled thành true GMTDVehicleReporter trong ứng dụng trình điều khiển. Tiếp đến là lớp GMTDVehicleReporter tự động gửi thông tin cập nhật vị trí đến Fleet Engine. Sau Fleet Engine và các dịch vụ phụ trợ của khách hàng khớp và chỉ định chiếc xe cho một chuyến đi, Lớp GMTDVehicleReporter tự động gửi thông tin cập nhật về tuyến đường khi GMSNavigator đang ở chế độ điều hướng, tức là khi một đích đến được đặt thông qua setDestinations.

SDK trình điều khiển thiết lập tuyến đường để khớp với lộ trình điều hướng hiện tại của người lái xe. Người nhận đảm bảo thông tin cập nhật vị trí chính xác, đặt điểm tham chiếu trong setDestinations để khớp điểm đến trong Fleet Engine.

Ví dụ sau đây trình bày cách bật tính năng cập nhật vị trí:

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

Đặt khoảng thời gian giữa những lần cập nhật

Theo mặc định, khi bạn đặt locationTrackingEnabled thành true, SDK trình điều khiển gửi thông tin cập nhật về chuyến đi và phương tiện đến Fleet Engine mỗi 10 giây. Bạn có thể thay đổi khoảng thời gian cập nhật bằng locationUpdateInterval thành một lần cập nhật tối thiểu 5 giây hoặc tối đa 60 giây. Bản cập nhật thường xuyên hơn có thể dẫn đến yêu cầu và lỗi chậm hơn.

Đặt trạng thái của xe thành "Trực tuyến"

Sau khi bạn bật tính năng cập nhật vị trí, hãy đặt trạng thái của xe thành ONLINE để làm cho xe có sẵn cho các cụm từ tìm kiếm trong Fleet Engine.

Các ví dụ sau đây minh hoạ cách đặt trạng thái của xe thành ONLINE. Để chi tiết, hãy xem 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

Các bước tiếp theo

Đặt thông tin chi tiết về chuyến đi