Phần này trình bày cách chuẩn bị xe cho chuyến đi. Bạn phải hoàn tất từng bước sau đây thì phần phụ trợ mới có thể khớp xe với chuyến đi.
Thiết lập trình nghe
Sau khi khởi chạy Driver SDK và tạo thực thể GMTDRidesharingDriverAPI, bạn có thể thiết lập trình nghe sự kiện để theo dõi việc cập nhật xe thành công hay không thành công đượ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 dành cho tài xế, chẳng hạn như thông báo cho tài xế nếu quá trình giao tiếp với phần phụ trợ của bạn không thành công.
Theo dõi các sự kiện cập nhật xe
Khi tài xế bật tính năng thông báo cập nhật vị trí trong ứng dụng dành cho tài xế, Driver SDK 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 GMTDVehicleReporter. Bạn có thể yêu cầu ứ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:didSucceedVehicleUpdateThông báo cho ứng dụng dành cho tài xế rằng các dịch vụ phụ trợ đã nhận thành công 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 quá trình cập nhật xe không thành công. Miễn là tài xế đã bật tính năng thông báo cập nhật vị trí, lớp
GMTDVehicleReportersẽ tiếp tục gửi dữ liệu mới nhất đến Fleet Engine.
Các 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.
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 báo cập nhật vị trí của xe
Navigation SDK cung cấp thông tin cập nhật về vị trí cho Driver SDK 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 đặt locationTrackingEnabled thành true trên GMTDVehicleReporter trong ứng dụng dành cho tài xế. Sau đó, lớp GMTDVehicleReporter sẽ tự động gửi thông tin cập nhật về vị trí đến Fleet Engine. Sau khi các dịch vụ phụ trợ của Fleet Engine và khách hàng khớp và chỉ định xe cho một chuyến đi, lớp GMTDVehicleReporter sẽ tự động gửi thông tin cập nhật về tuyến đường khi GMSNavigator ở chế độ thao tác, tức là khi một điểm đến được thiết lập thông qua setDestinations.
Driver SDK thiết lập tuyến đường cho khớp với đường đi hiện tại của tài xế. Để đảm bảo thông báo cập nhật vị trí chính xác, hãy đặt điểm tham chiếu trong setDestinations cho khớp với điểm đến trong Fleet Engine.
Ví dụ sau đây cho thấy cách bật tính năng 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.
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, Driver SDK sẽ gửi thông tin cập nhật về chuyến đi và xe đế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 chậm hơn và lỗi.
Đặt trạng thái xe thành trực tuyến
Sau khi 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ể được tìm kiếm trong Fleet Engine.
Các ví dụ sau đây cho thấy cách đặt trạng thái 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
Bước tiếp theo
Thiết lập thông tin chi tiết về chuyến đi