כשעוקבים אחרי נסיעה, האפליקציה לצרכנים מציגה את המיקום של את הרכב המתאים לצרכן. כדי לעשות זאת, האפליקציה צריכה להפעיל את לעקוב אחרי נסיעה, לעדכן את ההתקדמות בנסיעה ולהפסיק את המעקב אחרי נסיעה שהושלמו.
במסמך הזה מוסבר איך התהליך הזה עובד.
התחלת מעקב אחר נסיעה
כך מתחילים לעקוב אחרי נסיעה באמצעות שיתוף מסלול:
לאסוף את כל נתוני המשתמשים, כמו מיקומי נטישה ואיסוף עצמי מ-
ViewController
.צריך ליצור
ViewController
חדש כדי להתחיל את שיתוף התהליך ישירות.
הדוגמה הבאה מראה איך להתחיל לשתף את הנסיעה מיד אחרי טעינות של צפיות.
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];
}
הפסקת המעקב אחרי נסיעה
המעקב אחרי נסיעה נפסק אחרי שהיא מסתיימת או מבוטלת. הבאים דוגמה שמראה איך להפסיק את השיתוף של הנסיעה הפעילה.
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];
}
עדכון ההתקדמות בנסיעה
במהלך נסיעה, תוכלו לנהל את ההתקדמות שלכם בנסיעה באופן הבא:
אפשר להתחיל להאזין לעדכונים. לדוגמה, אפשר להיכנס אל לדוגמה, אפשר להתחיל להאזין לעדכונים.
מטפלים בעדכוני נסיעה. לדוגמה, אפשר להיכנס אל דוגמה לעדכוני נסיעה
כשנסיעה מסתיימת או מבוטלת, אפשר להפסיק להאזין לעדכונים. לדוגמה, אפשר להיכנס אל לדוגמה, אפשר להפסיק להאזין לעדכונים כדי לקבל עדכונים.
דוגמה לקבלת עדכונים להתחלת ההאזנה
הדוגמה הבאה מראה איך לרשום את הקריאה החוזרת של 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];
...
}
דוגמה להפסקת ההאזנה לעדכונים
הדוגמה הבאה מראה איך לבטל את הרישום של tripModel
קריאה חוזרת.
Swift
/*
* MapViewController.swift
*/
deinit {
self.currentTripModel.unregisterSubscriber(self)
}
Objective-C
/*
* MapViewController.m
*/
- (void)dealloc {
[self.currentTripModel unregisterSubscriber:self];
...
}
דוגמה לעדכוני נסיעה
הדוגמה הבאה מראה איך להטמיע את GMTCTripModelSubscriber
לטיפול בקריאות חוזרות (callback) כשמצב הנסיעה מעודכן.
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.
}
טיפול בשגיאות בנסיעה
אם נרשמת ל-tripModel
ואירעה שגיאה, אפשר לקבל את הקריאה החוזרת
של tripModel
על ידי הטמעת השיטה להענקת גישה
tripModel(_:didFailUpdateTripWithError:)
. שגיאה
ההודעות עומדות בתקן Google Cloud Error. לפירוט השגיאה
הגדרות ההודעות וכל קודי השגיאה, עיינו ב
מסמכי תיעוד של שגיאות Google Cloud
ריכזנו כאן כמה שגיאות נפוצות שעשויות להתרחש במהלך מעקב אחרי נסיעות:
HTTP | הכנסה לקליק | תיאור |
---|---|---|
400 | INVALID_ARGUMENT | הלקוח ציין שם נסיעה לא חוקי. שם הנסיעה חייב להופיע אחרי המחרוזת.
פורמט providers/{provider_id}/trips/{trip_id} .
provider_id חייב להיות המזהה של הפרויקט בענן שבבעלות
ספק השירות. |
401 | לא מאומת | השגיאה הזו תופיע אם אין פרטי כניסה תקינים לאימות. לדוגמה, אם אסימון ה-JWT חתום ללא מזהה נסיעה או אסימון ה-JWT פג תוקף. |
403 | PERMISSION_DENIED | השגיאה הזו מופיעה אם ללקוח אין הרשאה מספקת (למשל, משתמש בתפקיד צרכן מנסה לקרוא ל-updateTrip), אם אסימון ה-JWT לא חוקי, או שה-API לא הופעל עבור פרויקט הלקוח. יכול להיות שאסימון ה-JWT חסר או שהוא חתום עם מזהה נסיעה לא תואם למזהה הנסיעה המבוקש. |
429 | RESOURCE_EXHAUSTED | מכסת המשאבים היא אפס או שקצב התנועה חורג מהמגבלה. |
503 | UNAVAILABLE | השירות לא זמין. בדרך כלל השרת מושבת. |
504 | DEADLINE_EXCEEDED | המועד האחרון של הבקשה עבר. השגיאה הזו מתרחשת רק אם המתקשר מגדיר שהוא קצר יותר מהמועד האחרון שנקבע כברירת מחדל לשיטה (כלומר, המועד האחרון המבוקש אינו מספיק כדי שהשרת יוכל לעבד את הבקשה) הבקשה לא הסתיימה במסגרת המועד האחרון. |
טיפול בשגיאות SDK של צרכנים
ה-SDK לצרכנים שולח שגיאות בעדכון הנסיעה לאפליקציה לצרכנים באמצעות קריאה חוזרת (callback)
על מנגנוני תשומת לב. פרמטר הקריאה החוזרת (callback) הוא סוג החזרה ספציפי לפלטפורמה (
TripUpdateError
ב-Android,
NSError
ב-iOS).
חילוץ קודי סטטוס
השגיאות שמועברות לקריאה החוזרת הן בדרך כלל שגיאות gRPC, ואפשר גם לחלץ מהם מידע נוסף בצורת קוד סטטוס. עבור לרשימה המלאה של קודי הסטטוס, קודי סטטוס והשימוש בהם ב-gRPC.
Swift
ניתן להתקשר חזרה אל NSError
בעוד 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
בעוד 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;
...
}
}
פירוש של קודי סטטוס
קודי הסטטוס מכסים שני סוגים של שגיאות: שגיאות הקשורות לשרת ולרשת, שגיאות בצד הלקוח.
שגיאות בחיבור לשרת ולרשת
קודי המצב הבאים מיועדים לשגיאות רשת או לשגיאות שרת, אינה נדרשת בפעולה כלשהי כדי לפתור אותן. Consumer SDK באופן אוטומטי מתאושש מהם.
קוד סטטוס | תיאור |
---|---|
בוצעה הפרה | השרת הפסיק לשלוח את התגובה. בדרך כלל הדבר נגרם על ידי בעיה בשרת. |
בוטלה | השרת סיים את התגובה היוצאת. זה בדרך כלל
קורה כאשר
האפליקציה נשלחת לרקע, או כאשר חל שינוי במצב אפליקציה לצרכן. |
INTERRUPTED | |
DEADLINE_EXCEEDED | לשרת נדרש זמן רב מדי להגיב. |
UNAVAILABLE | השרת לא היה זמין. בדרך כלל הסיבה לכך היא רשת . |
שגיאות לקוח
קודי הסטטוס הבאים מיועדים לשגיאות לקוח, ועליך לנקוט פעולה כדי לפתור אותן. ה-SDK של הצרכן ממשיך לנסות לרענן את הנסיעה עד לסיים את השיתוף של התהליך, אבל הוא לא ישוחזר עד שתנקטו פעולה.
קוד סטטוס | תיאור |
---|---|
INVALID_ARGUMENT | באפליקציה לצרכנים צוין שם נסיעה לא תקין. שם הנסיעה צריך להיות
בפורמט providers/{provider_id}/trips/{trip_id} .
|
NOT_FOUND | הנסיעה מעולם לא נוצרה. |
PERMISSION_DENIED | לאפליקציה לצרכנים אין הרשאות מספיקות. השגיאה הזו מתרחשת כאשר:
|
RESOURCE_EXHAUSTED | מכסת המשאבים היא אפס, או שקצב זרימת התנועה חורג מגבלת מהירות. |
לא מאומת | האימות של הבקשה נכשל כי אסימון JWT לא תקין. הזה כאשר אסימון ה-JWT חתום ללא מזהה נסיעה, או כשפג התוקף של אסימון ה-JWT. |