Phần này hướng dẫn cách chuẩn bị xe cho chuyến đi. Bạn phải hoàn tất từng bước sau đây trước khi hệ thống phụ trợ 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 Driver SDK và tạo một thực thể 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 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à máy chủ phụ trợ của khách hàng thông qua lớp GMTDVehicleReporter
. 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: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
GMTDVehicleReporter
sẽ 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
Nghe thông tin cập nhật về 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 thông báo 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ị trí đến Fleet Engine. Sau khi Fleet Engine và các dịch vụ phụ trợ của 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ế độ điều hướng, tức là khi một điểm đến được đặt thông qua setDestinations
.
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
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.
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 cho 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.
Đặt trạng thái xe thành đang hoạt động
Sau khi bạn bật thông tin 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 trên 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
Đặt thông tin chi tiết về chuyến đi