ติดตามการเดินทางใน iOS

เลือกแพลตฟอร์ม Android iOS JavaScript

เมื่อคุณติดตามการเดินทาง แอปสำหรับผู้บริโภคจะแสดงตำแหน่งของ ยานพาหนะที่เหมาะสมให้กับผู้บริโภค โดยแอปของคุณต้องเริ่มต้น ติดตามการเดินทาง อัปเดตความคืบหน้าการเดินทาง และหยุดติดตามการเดินทางเมื่อการเดินทาง เสร็จสมบูรณ์

เอกสารนี้กล่าวถึงวิธีการทำงานของกระบวนการดังกล่าว

เริ่มติดตามการเดินทาง

วิธีเริ่มติดตามการเดินทางโดยใช้การแชร์การเดินทางมีดังนี้

  • รวบรวมข้อมูลทั้งหมดจากผู้ใช้ เช่น จุดส่งและจุดรับ จาก 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];
}

อัปเดตความคืบหน้าของการเดินทาง

ในระหว่างการเดินทาง คุณจะจัดการความคืบหน้าของการเดินทางได้ดังนี้

เมื่อการเดินทางเสร็จสมบูรณ์หรือยกเลิกไปแล้ว ให้หยุดการฟังข้อมูลอัปเดต ดูตัวอย่างได้ที่ หยุดฟังตัวอย่างการอัปเดต

ตัวอย่างการเริ่มฟังการอัปเดต

ตัวอย่างต่อไปนี้แสดงวิธีลงทะเบียน Callback 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 Callback

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 สำหรับข้อผิดพลาดโดยละเอียด คำจำกัดความข้อความและรหัสข้อผิดพลาดทั้งหมด โปรดดู เอกสารประกอบเกี่ยวกับข้อผิดพลาดของ Google Cloud

ข้อผิดพลาดที่พบบ่อยที่อาจเกิดขึ้นระหว่างการตรวจสอบการเดินทางมีดังนี้

HTTP RPC คำอธิบาย
400 ไม่ถูกต้อง ไคลเอ็นต์ระบุชื่อการเดินทางไม่ถูกต้อง ชื่อการเดินทางต้องเป็นไปตาม รูปแบบ providers/{provider_id}/trips/{trip_id} provider_id ต้องเป็นรหัสของโปรเจ็กต์ระบบคลาวด์ที่เป็นของ ผู้ให้บริการ
401 ไม่มีการตรวจสอบสิทธิ์ คุณจะได้รับข้อผิดพลาดนี้หากไม่มีข้อมูลเข้าสู่ระบบการตรวจสอบสิทธิ์ที่ถูกต้อง เช่น หากโทเค็น JWT มีการรับรองโดยไม่มีรหัสการเดินทางหรือโทเค็น JWT หมดอายุแล้ว
403 PERMISSION_DENIED คุณจะได้รับข้อผิดพลาดนี้หากไคลเอ็นต์มีสิทธิ์ไม่เพียงพอ (ตัวอย่างเช่น ผู้ใช้ที่มีบทบาทผู้บริโภคจะพยายามเรียกใช้ updateTrip) หาก โทเค็น JWT ไม่ถูกต้อง หรือ API ไม่ได้เปิดใช้สำหรับโปรเจ็กต์ไคลเอ็นต์ โทเค็น JWT อาจหายไปหรือลงนามโทเค็นด้วยรหัสการเดินทางที่ ไม่ตรงกับรหัสการเดินทางที่ขอ
429 RESOURCE_EXHAUSTED โควต้าทรัพยากรอยู่ที่ 0 หรือปริมาณการเข้าชมเกินขีดจำกัด
503 UNAVAILABLE ไม่พร้อมให้บริการ โดยทั่วไปแล้ว เซิร์ฟเวอร์ขัดข้อง
504 DEADLINE_EXCEEDED เกินกำหนดเวลาในการส่งคำขอแล้ว ข้อผิดพลาดนี้จะเกิดขึ้นเฉพาะในกรณีที่ผู้เรียกใช้ตั้งค่า ซึ่งสั้นกว่ากำหนดเวลาเริ่มต้นของวิธี (กล่าวคือ กำหนดเวลาที่ขอไม่เพียงพอที่เซิร์ฟเวอร์จะดำเนินการตามคำขอ) และ คำขอดำเนินการไม่เสร็จภายในระยะเวลาที่กำหนด

จัดการข้อผิดพลาด SDK ของผู้บริโภค

Consumer SDK จะส่งข้อผิดพลาดในการอัปเดตการเดินทางไปยังแอปสำหรับผู้บริโภคโดยใช้ Callback Google Analytics พารามิเตอร์ Callback เป็นประเภทการแสดงผลเฉพาะแพลตฟอร์ม ( TripUpdateError ใน Android และ NSError บน iOS)

ดึงรหัสสถานะ

ข้อผิดพลาดที่ส่งไปยัง Callback มักจะเป็นข้อผิดพลาด 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;
    ...
  }
}

ตีความรหัสสถานะ

รหัสสถานะครอบคลุมข้อผิดพลาดสองประเภท ได้แก่ ข้อผิดพลาดเกี่ยวกับเซิร์ฟเวอร์และเครือข่าย และ ข้อผิดพลาดฝั่งไคลเอ็นต์

ข้อผิดพลาดเกี่ยวกับเซิร์ฟเวอร์และเครือข่าย

รหัสสถานะต่อไปนี้มีไว้สำหรับข้อผิดพลาดเกี่ยวกับเครือข่ายหรือเซิร์ฟเวอร์ และคุณ ไม่จำเป็นต้องดำเนินการใดๆ เพื่อแก้ไข SDK ของผู้บริโภคโดยอัตโนมัติ การกู้คืน

รหัสสถานะคำอธิบาย
ล้มเลิกแล้ว เซิร์ฟเวอร์หยุดส่งการตอบกลับ ซึ่งโดยปกติจะเกิดจาก เซิร์ฟเวอร์มีปัญหา
ยกเลิกแล้ว เซิร์ฟเวอร์สิ้นสุดการตอบกลับขาออก โดยปกติ เกิดขึ้นเมื่อ
ส่งแอปไปที่พื้นหลัง หรือเมื่อมีการเปลี่ยนสถานะในส่วน
แอปสำหรับผู้บริโภค
INTERRUPTED
DEADLINE_EXCEEDED เซิร์ฟเวอร์ใช้เวลาตอบสนองนานเกินไป
UNAVAILABLE เซิร์ฟเวอร์ไม่พร้อมใช้งาน ซึ่งโดยปกติจะเกิดจากเครือข่าย ปัญหา

ข้อผิดพลาดของไคลเอ็นต์

รหัสสถานะต่อไปนี้มีไว้สำหรับข้อผิดพลาดของไคลเอ็นต์ และคุณต้องดำเนินการเพื่อ แก้ปัญหาเหล่านั้น Consumer SDK จะพยายามรีเฟรชการเดินทางต่อไปจนกว่าคุณจะ หยุดการแชร์เส้นทาง แต่จะไม่กู้คืนจนกว่าคุณจะดำเนินการ

รหัสสถานะคำอธิบาย
ไม่ถูกต้อง แอป Consumer ระบุชื่อการเดินทางไม่ถูกต้อง ชื่อการเดินทางต้อง ใช้รูปแบบ providers/{provider_id}/trips/{trip_id}
NOT_FOUND ไม่เคยสร้างการเดินทางนี้
PERMISSION_DENIED แอป Consumer มีสิทธิ์ไม่เพียงพอ ข้อผิดพลาดนี้เกิดขึ้นเมื่อ
  • แอป Consumer ไม่มีสิทธิ์
  • ไม่ได้เปิดใช้ SDK ของผู้บริโภคสำหรับโปรเจ็กต์ใน Google Cloud คอนโซล
  • ไม่มีโทเค็น JWT หรือไม่ถูกต้อง
  • โทเค็น JWT ลงนามด้วยรหัสการเดินทางที่ไม่ตรงกับ การเดินทางที่ขอ
RESOURCE_EXHAUSTED โควต้าทรัพยากรอยู่ที่ 0 หรืออัตราการไหลเวียนของการรับส่งข้อมูลเกิน ขีดจำกัดความเร็ว
ไม่มีการตรวจสอบสิทธิ์ คำขอตรวจสอบสิทธิ์ไม่สำเร็จเนื่องจากโทเค็น JWT ไม่ถูกต้อง ช่วงเวลานี้ จะเกิดขึ้นเมื่อโทเค็น JWT ลงนามโดยไม่มีรหัสการเดินทาง หรือ เมื่อโทเค็น JWT หมดอายุ