เตรียมยานพาหนะให้พร้อม

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

ตั้งค่า Listener

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

ฟังเหตุการณ์การอัปเดตยานพาหนะ

เมื่อคนขับเปิดใช้การอัปเดตตำแหน่งในแอปไดรเวอร์ SDK ไดรเวอร์ ส่งการอัปเดตยานพาหนะไปยัง Fleet Engine และลูกค้าเป็นประจำ ผ่านคลาส GMTDVehicleReporter คุณให้แอปตอบกลับได้ เพื่ออัปเดตกิจกรรมโดยการตั้งค่าโปรโตคอล GMTDVehicleReporterListener

เมื่อใช้ GMTDVehicleReporterListener คุณจะจัดการเหตุการณ์ต่อไปนี้ได้

  • vehicleReporter:didSucceedVehicleUpdate

    แจ้งแอปไดรเวอร์ว่าบริการแบ็กเอนด์ได้รับ ตำแหน่งรถและการอัปเดตสถานะ

  • vehicleReporter:didFailVehicleUpdate:withError

    แจ้งผู้ฟังว่าการอัปเดตยานพาหนะล้มเหลว ตราบใดที่คนขับ เปิดใช้การอัปเดตตำแหน่งแล้ว ชั้นเรียน GMTDVehicleReporter ยังคงส่งต่อไป ข้อมูลล่าสุดไปยัง Fleet Engine

ตัวอย่างต่อไปนี้แสดงวิธีการตั้งค่า 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 จะอัปเดตตำแหน่งให้กับ Driver SDK ผ่าน GMSRoadSnappedLocationProvider ชั้นเรียน หากต้องการรับการอัปเดตดังกล่าว คุณต้องตั้งค่า เพิ่ม 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

เปิดใช้การอัปเดตตำแหน่ง

หากต้องการเปิดใช้การอัปเดตตำแหน่ง ให้ตั้งค่า locationTrackingEnabled เป็น true GMTDVehicleReporter ในแอปไดรเวอร์ จากนั้น ชั้นเรียน GMTDVehicleReporter ส่งการอัปเดตตำแหน่งไปยัง Fleet Engine โดยอัตโนมัติ หลังจาก Fleet Engine และ บริการแบ็กเอนด์ของลูกค้าจะจับคู่และกำหนดพาหนะให้กับการเดินทาง GMTDVehicleReporter คลาสจะส่งการอัปเดตเส้นทางโดยอัตโนมัติเมื่อ GMSNavigator อยู่ในโหมดการนำทาง ซึ่งเป็นเวลากำหนดจุดหมาย setDestinations

Driver SDK กำหนดเส้นทางให้ตรงกับเส้นทางการนำทางปัจจุบันของผู้ขับ ถึง เพื่อให้มีการอัปเดตตำแหน่งที่ถูกต้อง ตั้งค่าจุดอ้างอิงใน setDestinations ให้เหมือนกับส่วนที่เลือก ปลายทางใน Fleet Engine

ตัวอย่างต่อไปนี้แสดงวิธีเปิดใช้งานการอัปเดตตำแหน่ง

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 ส่งการอัปเดตการเดินทางและยานพาหนะไปยัง Fleet Engine ทุกๆ 10 วินาที คุณสามารถ เปลี่ยนช่วงเวลาการอัปเดตโดยใช้ locationUpdateInterval ให้มีค่าอัปเดตน้อยที่สุด ช่วง 5 วินาที หรือไม่เกิน 60 วินาที การอัปเดตบ่อยขึ้นอาจ จะทำให้ส่งคำขอและข้อผิดพลาดได้ช้าลง

ตั้งค่าสถานะยานพาหนะเป็นออนไลน์

หลังจากเปิดใช้การอัปเดตตำแหน่งแล้ว ให้ตั้งค่าสถานะยานพาหนะเป็น ONLINE เพื่อทําการตั้งค่า ยานพาหนะที่พร้อมใช้งานสำหรับคำค้นหาใน Fleet Engine

ตัวอย่างต่อไปนี้แสดงวิธีตั้งค่าสถานะยานพาหนะเป็น 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

ขั้นตอนถัดไป

กำหนดรายละเอียดการเดินทาง