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

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 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 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 sẽ gửi các bản cập nhật xe định kỳ cho Fleet Engine và phần phụ trợ của khách hàng thông qua Lớp GMTDDeliveryVehicleReporter. Bạn có thể yêu cầu ứng dụng phản hồi để 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 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 GMTDDeliveryVehicleReporter sẽ tiếp tục để gửi dữ liệu mới nhất đến Fleet Engine.

Ví dụ sau đây trình bày 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.
    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 tính năng cập nhật vị trí

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

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, thiết lập điểm tham chiếu trong -setDestinations:callback: để khớp với đ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.
    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, SDK trình điều khiển gửi thông tin cập nhật vị trí tới Fleet Engine sau 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 khoảng thời gian 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 yêu cầu và lỗi chậm hơn.