Khi bạn theo dõi một chuyến đi, ứng dụng dành cho người tiêu dùng sẽ hiển thị vị trí của chiếc xe phù hợp cho người tiêu dùng. Để làm việc này, ứng dụng của bạn cần bắt đầu theo dõi một chuyến đi, cập nhật tiến trình chuyến đi và ngừng theo dõi một chuyến đi khi chuyến đi đó hoàn tất.
Tài liệu này trình bày cách hoạt động của quy trình đó.
Bắt đầu theo dõi một chuyến đi
Sau đây là cách bắt đầu theo dõi một chuyến đi:
Thu thập tất cả thông tin đầu vào của người dùng, chẳng hạn như vị trí trả hàng và vị trí đến lấy hàng từ
ViewController
.Tạo một
ViewController
mới để bắt đầu theo dõi trực tiếp một chuyến đi.
Ví dụ sau đây minh hoạ cách bắt đầu theo dõi một chuyến đi ngay sau khi chế độ xem tải.
Swift
/*
* MapViewController.swift
*/
override func viewDidLoad() {
super.viewDidLoad()
...
self.mapView = GMTCMapView(frame: UIScreen.main.bounds)
self.mapView.delegate = self
self.view.addSubview(self.mapView)
}
func mapViewDidInitializeCustomerState(_: GMTCMapView) {
self.mapView.pickupLocation = self.selectedPickupLocation
self.mapView.dropoffLocation = self.selectedDropoffLocation
self.startConsumerMatchWithLocations(
pickupLocation: self.mapView.pickupLocation!,
dropoffLocation: self.mapView.dropoffLocation!
) { [weak self] (tripName, error) in
guard let strongSelf = self else { return }
if error != nil {
// print error message.
return
}
let tripService = GMTCServices.shared().tripService
// Create a tripModel instance for listening the update of the trip
// specified by this trip name.
let tripModel = tripService.tripModel(forTripName: tripName)
// Create a journeySharingSession instance based on the tripModel
let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
// Add the journeySharingSession instance on the mapView for UI updating.
strongSelf.mapView.show(journeySharingSession)
// Register for the trip update events.
tripModel.register(strongSelf)
strongSelf.currentTripModel = tripModel
strongSelf.currentJourneySharingSession = journeySharingSession
strongSelf.hideLoadingView()
}
self.showLoadingView()
}
Objective-C
/*
* MapViewController.m
*/
- (void)viewDidLoad {
[super viewDidLoad];
...
self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
}
// Handle the callback when the GMTCMapView did initialized.
- (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {
self.mapView.pickupLocation = self.selectedPickupLocation;
self.mapView.dropoffLocation = self.selectedDropoffLocation;
__weak __typeof(self) weakSelf = self;
[self startTripBookingWithPickupLocation:self.selectedPickupLocation
dropoffLocation:self.selectedDropoffLocation
completion:^(NSString *tripName, NSError *error) {
__typeof(self) strongSelf = weakSelf;
GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
// Create a tripModel instance for listening to updates to the trip specified by this trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
// Create a journeySharingSession instance based on the tripModel.
GMTCJourneySharingSession *journeySharingSession =
[[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
// Add the journeySharingSession instance on the mapView for updating the UI.
[strongSelf.mapView showMapViewSession:journeySharingSession];
// Register for trip update events.
[tripModel registerSubscriber:self];
strongSelf.currentTripModel = tripModel;
strongSelf.currentJourneySharingSession = journeySharingSession;
[strongSelf hideLoadingView];
}];
[self showLoadingView];
}
Ngừng theo dõi chuyến đi
Bạn sẽ dừng theo dõi một chuyến đi khi chuyến đi đó hoàn tất hoặc bị huỷ. Ví dụ sau đây cho thấy cách dừng chia sẻ chuyến đi đang hoạt động.
Swift
/*
* MapViewController.swift
*/
func cancelCurrentActiveTrip() {
// Stop the tripModel
self.currentTripModel.unregisterSubscriber(self)
// Remove the journey sharing session from the mapView's UI stack.
self.mapView.hide(journeySharingSession)
}
Objective-C
/*
* MapViewController.m
*/
- (void)cancelCurrentActiveTrip {
// Stop the tripModel
[self.currentTripModel unregisterSubscriber:self];
// Remove the journey sharing session from the mapView's UI stack.
[self.mapView hideMapViewSession:journeySharingSession];
}
Cập nhật tiến trình chuyến đi
Trong chuyến đi, bạn có thể quản lý tiến trình của chuyến đi như sau:
Bắt đầu nghe thông tin cập nhật. Để biết ví dụ, hãy xem phần Ví dụ về cách bắt đầu nghe các bản cập nhật.
Xử lý mọi thông tin cập nhật về chuyến đi. Để xem ví dụ, hãy xem phần Ví dụ về cách xử lý thông tin cập nhật về chuyến đi
Khi chuyến đi hoàn tất hoặc bị huỷ, hãy dừng theo dõi thông tin cập nhật. Để biết ví dụ, hãy xem phần Ví dụ về cách ngừng nghe thông tin cập nhật.
Ví dụ về lệnh bắt đầu nghe thông tin cập nhật
Ví dụ sau đây minh hoạ cách đăng ký lệnh gọi lại tripModel
.
Swift
/*
* MapViewController.swift
*/
override func viewDidLoad() {
super.viewDidLoad()
// Register for trip update events.
self.currentTripModel.register(self)
}
Objective-C
/*
* MapViewController.m
*/
- (void)viewDidLoad {
[super viewDidLoad];
// Register for trip update events.
[self.currentTripModel registerSubscriber:self];
...
}
Ví dụ về lệnh dừng nghe thông tin cập nhật
Ví dụ sau đây cho thấy cách huỷ đăng ký lệnh gọi lại tripModel
.
Swift
/*
* MapViewController.swift
*/
deinit {
self.currentTripModel.unregisterSubscriber(self)
}
Objective-C
/*
* MapViewController.m
*/
- (void)dealloc {
[self.currentTripModel unregisterSubscriber:self];
...
}
Ví dụ về cách xử lý thông tin cập nhật về chuyến đi
Ví dụ sau đây cho thấy cách triển khai giao thức GMTCTripModelSubscriber
để xử lý lệnh gọi lại khi trạng thái chuyến đi được cập nhật.
Swift
/*
* MapViewController.swift
*/
func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
// Update the UI with the new `trip` data.
self.updateUI(with: trip)
}
func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
// Handle trip status did change.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRouteRemainingDistance activeRouteRemainingDistance: Int32) {
// Handle remaining distance of active route did update.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRoute activeRoute: [GMTSLatLng]?) {
// Handle trip active route did update.
}
func tripModel(_: GMTCTripModel, didUpdate vehicleLocation: GMTSVehicleLocation?) {
// Handle vehicle location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupLocation pickupLocation: GMTSTerminalLocation?) {
// Handle pickup location did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffLocation dropoffLocation: GMTSTerminalLocation?) {
// Handle drop off location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupETA pickupETA: TimeInterval) {
// Handle the pickup ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffETA dropoffETA: TimeInterval) {
// Handle the drop off ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateRemaining remainingWaypoints: [GMTSTripWaypoint]?) {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
func tripModel(_: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
// Handle the error.
}
func tripModel(_: GMTCTripModel, didUpdateIntermediateDestinations intermediateDestinations: [GMTSTerminalLocation]?) {
// Handle the intermediate destinations being updated.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRouteTraffic activeRouteTraffic: GMTSTrafficData?) {
// Handle trip active route traffic being updated.
}
Objective-C
/*
* MapViewController.m
*/
#pragma mark - GMTCTripModelSubscriber implementation
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateTrip:(nullable GMTSTrip *)trip
updatedPropertyFields:(enum GMTSTripPropertyFields)updatedPropertyFields {
// Update the UI with the new `trip` data.
[self updateUIWithTrip:trip];
...
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateTripStatus:(enum GMTSTripStatus)tripStatus {
// Handle trip status did change.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRouteRemainingDistance:(int32_t)activeRouteRemainingDistance {
// Handle remaining distance of active route did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
// Handle trip active route did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateVehicleLocation:(nullable GMTSVehicleLocation *)vehicleLocation {
// Handle vehicle location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdatePickupLocation:(nullable GMTSTerminalLocation *)pickupLocation {
// Handle pickup location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateDropoffLocation:(nullable GMTSTerminalLocation *)dropoffLocation {
// Handle drop off location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdatePickupETA:(NSTimeInterval)pickupETA {
// Handle the pickup ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateRemainingWaypoints:(nullable NSArray<GMTSTripWaypoint *> *)remainingWaypoints {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateDropoffETA:(NSTimeInterval)dropoffETA {
// Handle the drop off ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(nullable NSError *)error {
// Handle the error.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateIntermediateDestinations:
(nullable NSArray<GMTSTerminalLocation *> *)intermediateDestinations {
// Handle the intermediate destinations being updated.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRouteTraffic:(nullable GMTSTrafficData *)activeRouteTraffic {
// Handle trip active route traffic being updated.
}
Xử lý lỗi chuyến đi
Nếu đã đăng ký tripModel
và xảy ra lỗi, bạn có thể nhận lệnh gọi lại của tripModel
bằng cách triển khai phương thức uỷ quyền tripModel(_:didFailUpdateTripWithError:)
. Thông báo lỗi tuân theo tiêu chuẩn Lỗi của Google Cloud. Để biết định nghĩa chi tiết về thông báo lỗi và tất cả mã lỗi, hãy tham khảo tài liệu về Lỗi trên Google Cloud.
Sau đây là một số lỗi thường gặp có thể xảy ra trong quá trình giám sát chuyến đi:
HTTP | RPC | Mô tả |
---|---|---|
400 | INVALID_ARGUMENT | Ứng dụng khách chỉ định tên chuyến đi không hợp lệ. Tên chuyến đi phải tuân theo định dạng providers/{provider_id}/trips/{trip_id} . provider_id phải là mã nhận dạng của Dự án trên đám mây do nhà cung cấp dịch vụ sở hữu. |
401 | UNAUTHENTICATED | Bạn sẽ nhận được lỗi này nếu không có thông tin đăng nhập xác thực hợp lệ. Ví dụ: Nếu mã thông báo JWT được ký mà không có mã chuyến đi hoặc mã thông báo JWT đã hết hạn. |
403 | PERMISSION_DENIED | Bạn sẽ nhận được lỗi này nếu ứng dụng không có đủ quyền (ví dụ: người dùng có vai trò người tiêu dùng cố gắng gọi updateTrip), nếu mã thông báo JWT không hợp lệ hoặc API không được bật cho dự án ứng dụng. Có thể mã thông báo JWT bị thiếu hoặc mã thông báo được ký bằng mã chuyến đi không khớp với mã chuyến đi được yêu cầu. |
429 | RESOURCE_EXHAUSTED | Hạn mức tài nguyên bằng 0 hoặc tốc độ lưu lượng truy cập vượt quá giới hạn. |
503 | KHÔNG CÓ | Dịch vụ hiện không hoạt động. Thông thường, máy chủ sẽ không hoạt động. |
504 | DEADLINE_EXCEEDED | Đã quá hạn chót cho yêu cầu. Lỗi này chỉ xảy ra nếu phương thức gọi đặt thời hạn ngắn hơn thời hạn mặc định của phương thức (tức là thời hạn được yêu cầu không đủ để máy chủ xử lý yêu cầu) và yêu cầu không hoàn tất trong thời hạn. |
Xử lý lỗi Consumer SDK
Consumer SDK gửi các lỗi cập nhật chuyến đi đến ứng dụng người tiêu dùng bằng cơ chế gọi lại. Tham số callback là một kiểu trả về dành riêng cho nền tảng (TripUpdateError
trên Android và NSError
trên iOS).
Trích xuất mã trạng thái
Các lỗi được truyền đến lệnh gọi lại thường là lỗi gRPC và bạn cũng có thể trích xuất thêm thông tin từ các lỗi đó dưới dạng mã trạng thái. Để xem danh sách đầy đủ các mã trạng thái, hãy xem bài viết Mã trạng thái và cách sử dụng trong gRPC.
Swift
NSError
được gọi lại trong tripModel(_:didFailUpdateTripWithError:)
.
// Called when there is a trip update error.
func tripModel(_ tripModel: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
// Check to see if the error comes from gRPC.
if let error = error as NSError?, error.domain == "io.grpc" {
let gRPCErrorCode = error.code
...
}
}
Objective-C
NSError
được gọi lại trong tripModel:didFailUpdateTripWithError:
.
// Called when there is a trip update error.
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(NSError *)error {
// Check to see if the error comes from gRPC.
if ([error.domain isEqualToString:@"io.grpc"]) {
NSInteger gRPCErrorCode = error.code;
...
}
}
Diễn giải mã trạng thái
Mã trạng thái bao gồm 2 loại lỗi: lỗi liên quan đến máy chủ và mạng, cũng như lỗi phía máy khách.
Lỗi máy chủ và lỗi mạng
Các mã trạng thái sau đây là dành cho lỗi mạng hoặc lỗi máy chủ và bạn không cần phải làm gì để giải quyết các lỗi này. Consumer SDK sẽ tự động khôi phục từ các lỗi này.
Mã trạng thái | Mô tả |
---|---|
BỊ HUỶ | Máy chủ đã ngừng gửi phản hồi. Lỗi này thường là do sự cố máy chủ. |
ĐÃ HỦY | Máy chủ đã chấm dứt phản hồi đi. Điều này thường xảy ra khi ứng dụng được gửi xuống nền hoặc khi có sự thay đổi trạng thái trong ứng dụng Consumer. |
BỊ GIÁN ĐOẠN | |
DEADLINE_EXCEEDED | Máy chủ mất quá nhiều thời gian để phản hồi. |
KHÔNG CÓ | Máy chủ không hoạt động. Vấn đề này thường là do sự cố mạng. |
Lỗi máy khách
Các mã trạng thái sau đây là dành cho lỗi ứng dụng và bạn phải thực hiện hành động để giải quyết các lỗi này. Consumer SDK sẽ tiếp tục thử làm mới chuyến đi cho đến khi bạn kết thúc việc chia sẻ hành trình, nhưng SDK này sẽ không khôi phục được cho đến khi bạn thực hiện hành động.
Mã trạng thái | Mô tả |
---|---|
INVALID_ARGUMENT | Ứng dụng dành cho người tiêu dùng đã chỉ định một tên chuyến đi không hợp lệ; Tên chuyến đi phải tuân theo định dạng providers/{provider_id}/trips/{trip_id} .
|
NOT_FOUND | Chuyến đi chưa bao giờ được tạo. |
PERMISSION_DENIED | Ứng dụng Consumer không có đủ quyền. Lỗi này xảy ra khi:
|
RESOURCE_EXHAUSTED | Định mức tài nguyên bằng 0 hoặc tốc độ lưu lượng truy cập vượt quá giới hạn tốc độ. |
UNAUTHENTICATED | Yêu cầu không xác thực được do mã thông báo JWT không hợp lệ. Lỗi này xảy ra khi mã thông báo JWT được ký mà không có mã chuyến đi hoặc khi mã thông báo JWT đã hết hạn. |