本部分介绍了如何让车辆做好行程准备。您必须先完成以下每个步骤,然后后端才能将车辆与行程进行匹配。
设置监听器
初始化 Driver SDK 并创建 GMTDRidesharingDriverAPI
实例后,您可以设置事件监听器,以监控发送到车队引擎和后端的车辆更新是否成功。这些监听器可以在您的驾驶员应用中触发操作,例如在与后端通信失败时通知驾驶员。
监听车辆更新事件
当司机在司机应用中启用位置信息更新时,Driver SDK 会通过 GMTDVehicleReporter
类将定期车辆更新发送到车队引擎和客户后端。您可以通过设置 GMTDVehicleReporterListener
协议,让应用响应更新事件。
借助 GMTDVehicleReporterListener
,您可以处理以下事件:
vehicleReporter:didSucceedVehicleUpdate
告知 Driver 应用后端服务已成功接收车辆位置和状态更新。
vehicleReporter:didFailVehicleUpdate:withError
告知监听器车辆更新失败。只要驱动程序启用了位置信息更新,
GMTDVehicleReporter
类就会继续向车队引擎发送最新数据。
以下示例展示了如何设置 GMTDVehicleReporterListener
来处理这些事件:
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
监听车辆位置信息更新
Navigation SDK 通过 GMSRoadSnappedLocationProvider
类向 Driver SDK 提供位置信息更新。如需接收这些更新,您必须将 GMTDVehicleReporter
设置为监听器。
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
启用位置信息更新
如需启用位置信息更新,请在驾驶员应用的 GMTDVehicleReporter
上将 locationTrackingEnabled
设为 true
。然后,GMTDVehicleReporter
类会自动将位置信息更新发送到车队引擎。在车队引擎和客户后端服务匹配并将车辆分配给行程后,当 GMSNavigator
处于导航模式(即通过 setDestinations
设置目的地)时,GMTDVehicleReporter
类会自动发送路线更新。
Driver SDK 会设置路线,使其与驾驶员的当前导航路径相匹配。为确保位置信息准确更新,请在 setDestinations
中将航点设置为与车队引擎中的目的地一致。
以下示例展示了如何启用位置信息更新:
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
设置更新间隔
默认情况下,当您将 locationTrackingEnabled
设置为 true
时,Driver SDK 会以 10 秒的间隔向 Fleet Engine 发送行程和车辆更新。您可以使用 locationUpdateInterval
将更新间隔时间更改为最短 5 秒或最长 60 秒。提高更新频率可能会导致请求速度变慢和出现错误。
将车辆状态设为“在线”
启用位置信息更新后,请将车辆状态设置为 ONLINE
,以便车辆可在车队引擎中用于搜索查询。
以下示例展示了如何将车辆状态设置为 ONLINE
。如需了解详情,请参阅 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