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

Phần này cho biết cách chuẩn bị xe cho các 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 của bạ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 dành cho người lái xe, SDK dành cho người lái xe sẽ gửi thông tin cập nhật thường xuyên về xe cho Công cụ quản lý đội xe và phần phụ trợ của khách hàng 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 các 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 Trình điều khiển rằng các dịch vụ phụ trợ đã nhận được thành công thông tin cập nhật về vị trí và trạng thái của xe.

  • vehicleReporter:didFailVehicleUpdate:withError

    Thông báo cho trình nghe 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 thông tin cập nhật đó, bạn phải thiết lập GMTDVehicleReporter làm trình nghe.

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 đặt tuyến đường sao cho khớp với đường dẫn điều hướng hiện tại của trình điều khiển. Để đảm bảo thông tin cập nhật vị trí chính xác, hãy đặt điểm trung gian trong setDestinations khớp với đích đến trong Công cụ quản lý đội xe.

Ví dụ sau đây cho thấy cách bật thông tin 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 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. Việc cập nhật thường xuyên hơn có thể dẫn đến các yêu cầu chậm hơn và lỗi.

Đặt trạng thái 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 xe thành ONLINE để xe có thể xuất hiện trong các cụm từ tìm kiếm trong Công cụ quản lý đội xe.

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