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

Thiết lập trình nghe

Sau khi khởi chạy Driver SDK và tạo một thực thể GMTDDeliveryDriverAPI, 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 hoặc thất bại của các bản cập nhật 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 dành cho người lái xe, chẳng hạn như thông báo cho người lái xe nếu không giao tiếp được với phần phụ trợ của bạn.

Theo dõi các sự kiện cập nhật phương tiện

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, Driver SDK sẽ gửi thông tin cập nhật thường xuyên về xe cho Fleet Engine và phần phụ trợ của khách hàng thông qua lớp GMTDDeliveryVehicleReporter. Bạn có thể để ứng dụng phản hồi các sự kiện cập nhật 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 biết rằng các dịch vụ phụ trợ đã nhận được 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à trình điều khiển đã bật tính năng cập nhật vị trí, lớp GMTDDeliveryVehicleReporter sẽ tiếp tục gửi dữ liệu mới nhất đến Fleet Engine.

Ví dụ sau đây cho thấy cách thiết lập GMTDVehicleReporterListener để xử lý các sự kiện này:

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.
    deliveryDriverAPI.vehicleReporter.add(self)
  }

  func vehicleReporter(_ vehicleReporter: GMTDDeliveryVehicleReporter, didSucceed vehicleUpdate: GMTDVehicleUpdate) {
    // Handle update succeeded.
  }

  func vehicleReporter(_ vehicleReporter: GMTDDeliveryVehicleReporter, 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 IMPLEMENTED HAVE THE SAMPLE CODE UP TO THIS STEP.
  [delivervehicleReporter addListener:self];
}

- (void)vehicleReporter:(GMTDDeliveryVehicleReporter *)vehicleReporter didSucceedVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate {
  // Handle update succeeded.
}

- (void)vehicleReporter:(GMTDDeliveryVehicleReporter *)vehicleReporter didFailVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate withError:(NSError *)error {
  // Handle update failed.
}

@end

Bật thông báo cập nhật vị trí

Để bật tính năng cập nhật vị trí, trong ứng dụng dành cho người lái xe trên GMTDDeliveryVehicleReporter, hãy đặt locationTrackingEnabled thành YES. Sau đó, lớp GMTDDeliveryVehicleReporter sẽ tự động gửi thông tin cập nhật vị trí đến Fleet Engine. Ngoài ra, khi GMSNavigator ở chế độ điều hướng (tức là khi đích đến được đặt thông qua setDestinations), lớp GMTDDeliveryVehicleReporter sẽ tự động gửi thông tin cập nhật về tuyến đường và thời gian đến dự kiến.

Driver SDK thiết lập tuyến đường sao cho khớp với đường dẫn điều hướng hiện tại của tài xế. Để đảm bảo thông tin cập nhật vị trí chính xác, hãy đặt điểm tham chiếu trong -setDestinations:callback: sao cho khớp với đích đến trong Fleet Engine.

Ví dụ sau đây cho thấy cách bật thông báo 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.
    deliveryDriverAPI.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.
  deliveryDriverAPI.vehicleReporter.locationTrackingEnabled = YES;
}

@end

(Không bắt buộc) Đặt khoảng thời gian cập nhật

Theo mặc định, khi bạn đặt locationTrackingEnabled thành YES, Driver SDK sẽ gửi thông tin cập nhật vị trí đến Fleet Engine theo khoảng thời gian 10 giây. Bạn có thể thay đổi khoảng thời gian cập nhật bằng locationUpdateInterval thành khoảng thời gian cập nhật tối thiểu là 5 giây hoặc tối đa là 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 và lỗi chậm hơn.