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 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 không thành công 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. Các trình nghe này có thể kích hoạt các hành động trong ứng dụng của tài xế, chẳng hạn như thông báo cho tài xế nếu không thể giao tiếp với phần phụ trợ.
Theo dõi sự kiện cập nhật về xe
Khi tài xế bật thông báo cập nhật vị trí trong ứng dụng dành cho tài xế, SDK Tài xế sẽ gửi thông tin cập nhật thường xuyên về xe đến Fleet Engine và phần phụ trợ của khách hàng thông qua lớp GMTDDeliveryVehicleReporter. Bạn có thể thiết lập giao thức GMTDVehicleReporterListener để ứng dụng phản hồi các sự kiện cập nhật.
Với GMTDVehicleReporterListener, bạn có thể xử lý các sự kiện sau:
vehicleReporter:didSucceedVehicleUpdateThông báo cho ứng dụng Tài xế 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:withErrorThông báo cho trình nghe rằng không cập nhật được xe. Miễn là tài xế đã bật tính năng cập nhật vị trí, lớp
GMTDDeliveryVehicleReportersẽ 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 tài xế 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ế độ thao tác (tức là khi bạn đặt điểm đến 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 đặt tuyến đường khớp với đường đi 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 tham chiếu trong -setDestinations:callback: sao cho khớp với điểm đế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, SDK Trình điều khiển sẽ gửi thông báo 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 tăng tần suất cập nhật có thể khiến
yêu cầu chậm hơn và gây ra lỗi.