สร้างและแสดงการเดินทางกลุ่มที่ใช้ร่วมกัน

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

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

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

ขั้นตอนที่ 1 สร้างยานพาหนะใน Fleet Engine

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

คุณสามารถสร้างยานพาหนะโดยใช้วิธีใดวิธีหนึ่งต่อไปนี้

gRPC
เรียกใช้เมธอด CreateVehicle() ด้วยข้อความคำขอ CreateVehicleRequest คุณต้องมีสิทธิ์ของผู้ใช้ Fleet Engine ระดับสูงเพื่อโทรหา CreateVehicle()
REST
โทร https://fleetengine.googleapis.com/v1/providers.vehicles.create

คำเตือน

ข้อควรระวังต่อไปนี้จะปรากฏเมื่อคุณสร้างยานพาหนะ

  • อย่าลืมตั้งค่าสถานะเริ่มต้นของรถเป็น OFFLINE วิธีนี้ช่วยให้ Fleet Engine ค้นพบรถของคุณเพื่อจับคู่การเดินทางได้

  • provider_id ของยานพาหนะต้องตรงกับรหัสโปรเจ็กต์ของโปรเจ็กต์ Google Cloud ที่มีบัญชีบริการซึ่งใช้สำหรับการเรียกใช้ Fleet Engine แม้ว่าบัญชีบริการหลายบัญชีจะเข้าถึง Fleet Engine ของผู้ให้บริการบริการร่วมเดินทางรายเดียวกันได้ แต่ขณะนี้ Fleet Engine ยังไม่รองรับบัญชีบริการจากโปรเจ็กต์ Google Cloud ต่างๆ ที่เข้าถึงรถคันเดียวกัน

  • การตอบกลับที่แสดงผลจาก CreateVehicle() มีอินสแตนซ์ Vehicle ระบบจะลบอินสแตนซ์หลังจากผ่านไป 7 วันหากไม่มีการอัปเดตโดยใช้ UpdateVehicle() คุณควรโทรหา GetVehicle() ก่อนโทรหา CreateVehicle() เพื่อยืนยันว่าไม่มีรถอยู่แล้ว หาก GetVehicle() แสดงข้อผิดพลาดเกี่ยวกับ NOT_FOUND คุณควรโทรหา CreateVehicle() ดูข้อมูลเพิ่มเติมได้ที่ยานพาหนะและอายุการใช้งาน

ตัวอย่าง

ตัวอย่างรหัสผู้ให้บริการต่อไปนี้จะแสดงวิธีสร้างยานพาหนะใน Fleet Engine

static final String PROJECT_ID = "my-rideshare-co-gcp-project";

VehicleServiceBlockingStub vehicleService = VehicleService.newBlockingStub(channel);

String parent = "providers/" + PROJECT_ID;

Vehicle vehicle = Vehicle.newBuilder()
    .setVehicleState(VehicleState.OFFLINE)  // Initial state
    .addSupportedTripTypes(TripType.EXCLUSIVE)
    .setMaximumCapacity(4)
    .setVehicleType(VehicleType.newBuilder().setCategory(VehicleType.Category.AUTO))
    .build();

CreateVehicleRequest createVehicleRequest = CreateVehicleRequest.newBuilder()
    .setParent(parent)
    .setVehicleId("8241890")  // Vehicle ID assigned by solution provider.
    .setVehicle(vehicle)      // Initial state.
    .build();

// The Vehicle is created in the OFFLINE state, and no initial position is
// provided.  When the driver app calls the rideshare provider, the state can be
// set to ONLINE, and the driver app updates the vehicle location.
try {
  Vehicle createdVehicle = vehicleService.createVehicle(createVehicleRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}

หากต้องการสร้างVehicleที่รองรับการเดินทางพูลที่แชร์ คุณต้องเพิ่ม TripType.SHARED ลงในรายการประเภทการเดินทางที่รองรับในออบเจ็กต์ Vehicle ที่ส่งไปยัง CreateVehicleRequest

Vehicle vehicle = Vehicle.newBuilder()
    .setVehicleState(VehicleState.OFFLINE)
    .addSupportedTripTypes(TripType.SHARED)
    .setMaximumCapacity(4)
    .setVehicleType(VehicleType.newBuilder().setCategory(VehicleType.Category.AUTO))
    .build();

ขั้นตอนที่ 2 เปิดใช้การติดตามตำแหน่ง

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

คุณเปิดใช้การติดตามตำแหน่งสำหรับ Android และ iOS โดยทำดังนี้

  • เรียกใช้ Driver SDK สำหรับเมธอด Android enableLocationTracking()

  • ตั้งค่า Driver SDK สำหรับพร็อพเพอร์ตี้บูลีนของ iOS locationTrackingEnabled เป็น true

ตัวอย่าง

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

Java

RidesharingVehicleReporter vehicleReporter = ...;

vehicleReporter.enableLocationTracking();

Kotlin

val vehicleReporter = ...

vehicleReporter.enableLocationTracking()

Swift

vehicleReporter.locationTrackingEnabled = true

Objective-C

_vehicleReporter.locationTrackingEnabled = YES;

ขั้นตอนที่ 3 ตั้งค่าสถานะของรถให้ออนไลน์

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

คุณตั้งค่าสถานะของรถให้ออนไลน์สําหรับ Android และ iOS ดังนี้

ตัวอย่าง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งสถานะของรถเป็น ONLINE

Java

vehicleReporter.setVehicleState(VehicleState.ONLINE);

Kotlin

vehicleReporter.setVehicleState(VehicleState.ONLINE)

Swift

vehicleReporter.update(.online)

Objective-C

[_vehicleReporter updateVehicleState:GMTDVehicleStateOnline];

ขั้นตอนที่ 4 สร้างการเดินทางใน Fleet Engine

ในการสร้างการเดินทางร่วมที่ใช้ร่วมกัน คุณจะสร้างออบเจ็กต์ Trip ในลักษณะเดียวกับที่สร้างการเดินทางไปยังจุดหมายเดียว

การเดินทางคือวัตถุที่แสดงถึงการเดินทาง ซึ่งเป็นจุดพิกัดทางภูมิศาสตร์ของคอลเล็กชัน รวมถึงต้นทาง จุดอ้างอิง และจุดออกจาก คุณต้องสร้างออบเจ็กต์ Trip 1 รายการสำหรับคำขอการเดินทางแต่ละรายการ เพื่อให้จับคู่คำขอกับยานพาหนะและติดตามได้

  • คุณสร้างการเดินทางได้โดยเรียกใช้เมธอด CreateTrip() พร้อมข้อความคำขอ CreateTripRequest

ระบุแอตทริบิวต์ที่จำเป็น

ต้องกรอกข้อมูลในช่องต่อไปนี้เพื่อสร้างการเดินทางร่วม

parent
สตริงที่มีรหัสผู้ให้บริการ ซึ่งต้องเหมือนกับรหัสโปรเจ็กต์ของโปรเจ็กต์ Google Cloud ที่มีบัญชีบริการซึ่งใช้สำหรับการเรียกใช้ Fleet Engine
trip_id
สตริงที่คุณสร้างซึ่งระบุการเดินทางนี้แบบไม่ซ้ำ
trip
Trip ออบเจ็กต์ที่จะสร้าง

จำเป็นต้องตั้งค่าฟิลด์ต่อไปนี้ในออบเจ็กต์ Trip ที่ส่งไปยัง CreateTripRequest

trip_type
TripType.SHARED
pickup_point
จุดเริ่มต้นของการเดินทาง
dropoff_point
จุดส่งของการเดินทาง คุณไม่จำเป็นต้องกรอกข้อมูลในช่องนี้เมื่อสร้างการเดินทาง และจะตั้งค่าภายหลังได้โดยโทรไปที่ UpdateTrip

ตัวอย่าง

ตัวอย่างการผสานรวมแบ็กเอนด์ต่อไปนี้จะแสดงวิธีสร้างการเดินทางและ กำหนดยานพาหนะเป็นสระที่แชร์โดยอัตโนมัติ

// Vehicle with VEHICLE_ID ID is already created and it is assigned Trip A.

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "shared-trip-A";
static final String VEHICLE_ID = "your-vehicle-id";
static final String TRIP_A_ID = "trip-a-id";
static final String TRIP_B_ID = "trip-b-id";

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

String parent = "providers/" + PROJECT_ID;

LatLng tripBPickup =
    LatLng.newBuilder().setLatitude(-12.12314).setLongitude(88.142123).build();
LatLng tripBDropoff =
    LatLng.newBuilder().setLatitude(-14.12314).setLongitude(90.142123).build();

TerminalLocation tripBPickupTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBPickup).build();
TerminalLocation tripBDropoffTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBDropoff).build();

// TripA already exists and it's assigned to a vehicle with VEHICLE_ID ID.
Trip tripB = Trip.newBuilder()
    .setTripType(TripType.SHARED)
    .setVehicleId(VEHICLE_ID)
    .setPickupPoint(tripBPickupTerminalLocation)
    .setDropoffPoint(tripBDropoffTerminalLocation)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don’t specify an order, then the Fleet Engine adds Trip B’s
        // waypoints to the end of Trip A’s.
        ImmutableList.of(
            // Trip B’s pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripBPickupTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A’s drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B’s drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripBDropoffTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();

// Create Trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
    .setParent(parent)
    .setTripId(TRIP_B_ID)
    .setTrip(tripB)
    .build();

try {
  // createdTrip.remainingWaypoints will contain shared-pool waypoints.
  // [tripB.pickup, tripA.dropoff, tripB.dropoff]
  Trip createdTrip = tripService.createTrip(createTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}

ขั้นตอนที่ 5 อัปเดตการเดินทางโดยใช้รหัสรถและจุดอ้างอิง

คุณต้องกำหนดค่าการเดินทางด้วยรหัสรถเพื่อให้ Fleet Engine ติดตามรถตลอดเส้นทางได้

  • คุณอัปเดตการเดินทางด้วยรหัสรถยนต์ได้โดยโทรหาปลายทาง UpdateTrip ด้วย UpdateTripRequest ใช้ช่อง update_mask เพื่อระบุว่าคุณกำลังอัปเดตรหัสยานพาหนะ

คุณต้องเรียงลำดับกับจุดอ้างอิงที่ไม่มีการเข้าชมในคอลเล็กชันของจุดอ้างอิงบนยานพาหนะ (Trip.vehicle_waypoints) ของการเดินทาง โดย Fleet Engine จะใช้รายการนี้เพื่ออัปเดตจุดอ้างอิงในการเดินทางโดยอัตโนมัติสำหรับทุกการเดินทางในกลุ่มที่ใช้ร่วมกัน

เช่น ลองพิจารณาการเดินทางร่วม 2 ทริป ได้แก่ ทริป A และทริป B การเดินทาง A กำลังอยู่ระหว่าง สถานที่ส่งของ จากนั้นจึงเพิ่ม Trip B ในยานพาหนะเดิม ใน UpdateTripRequest 1 สำหรับการเดินทาง B คุณกำหนด vehicleId และตั้งค่า Trip.vehicle_waypoints เป็นลำดับจุดอ้างอิงที่เหมาะสมด้วย นั่นคือ B รับสินค้าจุดคืนB จุดคืน

  • การโทรไปยัง getVehicle() จะแสดงรายการจุดอ้างอิงที่เหลืออยู่ (remainingWaypoints) ที่มี
    B จุดรับจุดออกจากรถB การออกรถ
  • getTrip() หรือโค้ดเรียกกลับ onTripRemainingWaypointsUpdated สำหรับ Trip A จะแสดงรายการจุดอ้างอิงที่เหลืออยู่ (remainingWaypoints) ซึ่งมี B การรับสินค้าAการออก
  • getTrip() หรือโค้ดเรียกกลับ onTripRemainingWaypointsUpdated สำหรับ Trip B จะแสดงรายการจุดอ้างอิงที่เหลืออยู่ (remainingWaypoints) ที่มี B รับสินค้าจุดคืนB การออก

ตัวอย่าง

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

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_A_ID = "share-trip-A";
static final String TRIP_B_ID = "share-trip-B";
static final String VEHICLE_ID = "Vehicle";

String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_B_ID;

// Get Trip A and Trip B objects from either the Fleet Engine or storage.
Trip tripA = …;
Trip tripB = …;

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

// The trip settings to update.
Trip trip = Trip.newBuilder()
    .setVehicleId(VEHICLE_ID)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don’t specify an order, then the Fleet Engine adds Trip B’s
        // waypoints to the end of Trip A’s.
        ImmutableList.of(
            // Trip B’s pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getPickupPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A’s drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B’s drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getDropoffPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();

// The trip update request.
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
    .setName(tripName)
    .setTrip(trip)
    .setUpdateMask(FieldMask.newBuilder()
        .addPaths("vehicle_id")
        .addPaths("vehicle_waypoints"))
    .build();

// Error handling. If Fleet Engine has both a trip and vehicle with the IDs,
// and if the credentials validate, and if the given vehicle_waypoints list
// is valid, then the service updates the trip.
try {
  Trip updatedTrip = tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case NOT_FOUND:          // Either the trip or vehicle does not exist.
      break;
    case PERMISSION_DENIED:
      break;
    case INVALID_REQUEST:    // vehicle_waypoints is invalid.
      break;
  }
  return;
}

ขั้นตอนที่ 6 ฟังอัปเดตการเดินทางในแอปผู้บริโภค

  • สำหรับ Android คุณฟังการอัปเดตข้อมูลจากการเดินทางได้โดยรับออบเจ็กต์ TripModel จาก TripModelManager และลงทะเบียน Listener TripModelCallback

  • สำหรับ iOS คุณฟังข้อมูลอัปเดตจากการเดินทางได้โดยรับออบเจ็กต์ GMTCTripModel จาก GMTCTripService และลงทะเบียนสมาชิก GMTCTripModelSubscriber

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

ระบบจะเรียกใช้เมธอด TripModelCallback.onTripUpdated() และ tripModel(_:didUpdate:updatedPropertyFields:) เสมอ ไม่ว่าข้อมูลจะมีการเปลี่ยนแปลงหรือไม่ก็ตาม

ตัวอย่างที่ 1

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับ TripModel จาก TripModelManager/GMTCTripService และตั้งค่า Listener

Java

// Start journey sharing after a trip has been created via Fleet Engine.
TripModelManager tripModelManager = consumerApi.getTripModelManager();

// Get a TripModel object.
TripModel tripModel = tripModelManager.getTripModel(tripName);

// Register a listener on the trip.
TripModelCallback tripCallback = new TripModelCallback() {
  ...
};
tripModel.registerTripCallback(tripCallback);

// Set the refresh interval.
TripModelOptions tripModelOptions = TripModelOptions.builder()
    .setRefreshInterval(5000) // interval in milliseconds, so 5 seconds
    .build();
tripModel.setTripModelOptions(tripModelOptions);

// The trip stops auto-refreshing when all listeners are unregistered.
tripModel.unregisterTripCallback(tripCallback);

Kotlin

// Start journey sharing after a trip has been created via Fleet Engine.
val tripModelManager = consumerApi.getTripModelManager()

// Get a TripModel object.
val tripModel = tripModelManager.getTripModel(tripName)

// Register a listener on the trip.
val tripCallback = TripModelCallback() {
  ...
}

tripModel.registerTripCallback(tripCallback)

// Set the refresh interval.
val tripModelOptions =
  TripModelOptions.builder()
    .setRefreshInterval(5000) // interval in milliseconds, so 5 seconds
    .build()

tripModel.setTripModelOptions(tripModelOptions)

// The trip stops auto-refreshing when all listeners are unregistered.
tripModel.unregisterTripCallback(tripCallback)

Swift

let tripService = GMTCServices.shared().tripService

// Create a tripModel instance for listening for updates from the trip
// specified by the trip name.
let tripModel = tripService.tripModel(forTripName: tripName)

// Register for the trip update events.
tripModel.register(self)

// Set the refresh interval (in seconds).
tripModel.options.autoRefreshTimeInterval = 5

// Unregister for the trip update events.
tripModel.unregisterSubscriber(self)

Objective-C

GMTCTripService *tripService = [GMTCServices sharedServices].tripService;

// Create a tripModel instance for listening for updates from the trip
// specified by the trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];

// Register for the trip update events.
[tripModel registerSubscriber:self];

// Set the refresh interval (in seconds).
tripModel.options.autoRefreshTimeInterval = 5;

// Unregister for the trip update events.
[tripModel unregisterSubscriber:self];

ตัวอย่างที่ 2

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าผู้ฟัง TripModelCallback และสมาชิก GMTCTripModelSubscriber

Java

// Implements a callback for the trip model so your app can listen for trip
// updates from Fleet Engine.
TripModelCallback subscriber =
  new TripModelCallback() {

    @Override
    public void onTripStatusUpdated(TripInfo tripInfo, @TripStatus int status) {
      // ...
    }

    @Override
    public void onTripActiveRouteUpdated(TripInfo tripInfo, List<LatLng> route) {
      // ...
    }

    @Override
    public void onTripVehicleLocationUpdated(
        TripInfo tripInfo, @Nullable VehicleLocation vehicleLocation) {
      // ...
    }

    @Override
    public void onTripPickupLocationUpdated(
        TripInfo tripInfo, @Nullable TerminalLocation pickup) {
      // ...
    }

    @Override
    public void onTripPickupTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {
      // ...
    }

    @Override
    public void onTripDropoffLocationUpdated(
        TripInfo tripInfo, @Nullable TerminalLocation dropoff) {
      // ...
    }

    @Override
    public void onTripDropoffTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {
      // ...
    }

    @Override
    public void onTripETAToNextWaypointUpdated(
        TripInfo tripInfo, @Nullable Long timestampMillis) {
      // ...
    }

    @Override
    public void onTripActiveRouteRemainingDistanceUpdated(
        TripInfo tripInfo, @Nullable Integer distanceMeters) {
      // ...
    }

    @Override
    public void onTripUpdateError(TripInfo tripInfo, TripUpdateError error) {
      // ...
    }

    @Override
    public void onTripUpdated(TripInfo tripInfo) {
      // ...
    }

    @Override
    public void onTripRemainingWaypointsUpdated(
        TripInfo tripInfo, List<TripWaypoint> waypointList) {
      // ...
    }

    @Override
    public void onTripIntermediateDestinationsUpdated(
        TripInfo tripInfo, List<TerminalLocation> intermediateDestinations) {
      // ...
    }

    @Override
    public void onTripRemainingRouteDistanceUpdated(
        TripInfo tripInfo, @Nullable Integer distanceMeters) {
      // ...
    }

    @Override
    public void onTripRemainingRouteUpdated(TripInfo tripInfo, List<LatLng> route) {
      // ...
    }
  };

Kotlin

// Implements a callback for the trip model so your app can listen for trip
// updates from Fleet Engine.
val subscriber =
  object : TripModelCallback() {
    override fun onTripStatusUpdated(tripInfo: TripInfo, status: @TripStatus Int) {
      // ...
    }

    override fun onTripActiveRouteUpdated(tripInfo: TripInfo, route: List<LatLng>) {
      // ...
    }

    override fun onTripVehicleLocationUpdated(
      tripInfo: TripInfo,
      vehicleLocation: VehicleLocation?
    ) {
      // ...
    }

    override fun onTripPickupLocationUpdated(tripInfo: TripInfo, pickup: TerminalLocation?) {
      // ...
    }

    override fun onTripPickupTimeUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
      // ...
    }

    override fun onTripDropoffLocationUpdated(tripInfo: TripInfo, dropoff: TerminalLocation?) {
      // ...
    }

    override fun onTripDropoffTimeUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
      // ...
    }

    override fun onTripETAToNextWaypointUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
      // ...
    }

    override fun onTripActiveRouteRemainingDistanceUpdated(
      tripInfo: TripInfo,
      distanceMeters: Int?
    ) {
      // ...
    }

    override fun onTripUpdateError(tripInfo: TripInfo, error: TripUpdateError) {
      // ...
    }

    override fun onTripUpdated(tripInfo: TripInfo) {
      // ...
    }

    override fun onTripRemainingWaypointsUpdated(
      tripInfo: TripInfo,
      waypointList: List<TripWaypoint>
    ) {
      // ...
    }

    override fun onTripIntermediateDestinationsUpdated(
      tripInfo: TripInfo,
      intermediateDestinations: List<TerminalLocation>
    ) {
      // ...
    }

    override fun onTripRemainingRouteDistanceUpdated(tripInfo: TripInfo, distanceMeters: Int?) {
      // ...
    }

    override fun onTripRemainingRouteUpdated(tripInfo: TripInfo, route: List<LatLng>) {
      // ...
    }
  }

Swift

class TripModelSubscriber: NSObject, GMTCTripModelSubscriber {

  func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
    // Update the UI with the new `trip` data.
    updateUI(with: trip)
    ...
  }

  func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
    // Handle trip status did change.
  }

  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.
  }

  ...
}

Objective-C

@interface TripModelSubscriber : NSObject <GMTCTripModelSubscriber>
@end

@implementation TripModelSubscriber

- (void)tripModel:(GMTCTripModel *)tripModel
            didUpdateTrip:(nullable GMTSTrip *)trip
    updatedPropertyFields:(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
    didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
  // Handle trip 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.
}
…
@end

คุณเข้าถึงข้อมูลการเดินทางได้ทุกเมื่อดังนี้

  • เรียกใช้ Consumer SDK สำหรับเมธอด Android TripModel.getTripInfo() การเรียกใช้วิธีการนี้ไม่บังคับให้รีเฟรชข้อมูล แม้ว่าข้อมูลจะยังคงรีเฟรชตามความถี่ในการรีเฟรช

  • ดาวน์โหลดพร็อพเพอร์ตี้ Consumer SDK สำหรับ iOS GMTCTripModel.currentTrip

ขั้นตอนที่ 7 แสดงเส้นทางในแอปผู้บริโภค

คุณสามารถเข้าถึง API องค์ประกอบอินเทอร์เฟซผู้ใช้ของผู้ใช้การโดยสารและการนำส่ง ดังนี้

ตัวอย่าง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเริ่มอินเทอร์เฟซผู้ใช้ของการแชร์เส้นทาง

Java

JourneySharingSession session = JourneySharingSession.createInstance(tripModel);
consumerController.showSession(session);

Kotlin

val session = JourneySharingSession.createInstance(tripModel)
consumerController.showSession(session)

Swift

let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
mapView.show(journeySharingSession)

Objective-C

GMTCJourneySharingSession *journeySharingSession =
    [[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
[self.mapView showMapViewSession:journeySharingSession];

โดยค่าเริ่มต้น Consumer SDK จะแสดงเฉพาะจุดที่ใช้งานอยู่ในเส้นทาง แต่คุณมีตัวเลือกในการแสดงขาที่เหลือ ซึ่งรวมถึงปลายทางด้วย

ถ้าต้องการแสดงข้อมูลเกี่ยวกับจุดอ้างอิงจากการเดินทางอื่นๆ คุณสามารถเข้าถึงจุดอ้างอิงทั้งหมดที่เกี่ยวข้องกับการเดินทางนั้น ดังนี้

  • เรียกใช้ Consumer SDK สำหรับเมธอด Android TripModel.getTripInfo() จากนั้นโทรหา TripInfo.getRemainingWaypoints() เพื่อรับ TripWaypoint ออบเจ็กต์ TripWaypoint แต่ละรายการจะมีรหัสการเดินทาง ตำแหน่งจุดอ้างอิง และประเภทจุดอ้างอิง

  • ดาวน์โหลดพร็อพเพอร์ตี้ Consumer SDK สำหรับ iOS GMTCTripModel.currentTrip จากนั้นสร้างอาร์เรย์ GMTSTrip.remainingWaypoints เพื่อเข้าถึง GMTSTripWaypoint ออบเจ็กต์ GMTSTripWaypoint แต่ละรายการจะมีรหัสการเดินทาง ตำแหน่งจุดอ้างอิง และประเภทจุดอ้างอิง

ขั้นตอนที่ 8 จัดการสถานะการเดินทางใน Fleet Engine

คุณระบุสถานะการเดินทางโดยใช้ค่าแจกแจง TripStatus ค่าใดค่าหนึ่ง เมื่อสถานะของการเดินทางเปลี่ยนแปลง (เช่น เปลี่ยนจาก ENROUTE_TO_PICKUP เป็น ARRIVED_AT_PICKUP) คุณต้องอัปเดตสถานะการเดินทางผ่าน Fleet Engine สถานะการเดินทางจะเริ่มต้นด้วยค่า NEW เสมอ และลงท้ายด้วยค่า COMPLETE หรือ CANCELED ดูข้อมูลเพิ่มเติมได้ที่ trip_status

ตัวอย่าง

ตัวอย่างการผสานรวมแบ็กเอนด์ต่อไปนี้แสดงวิธีอัปเดตสถานะการเดินทางใน Fleet Engine

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "trip-8241890";

String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

// Trip settings to be updated.
Trip trip = Trip.newBuilder()
    .setTripStatus(TripStatus.ARRIVED_AT_PICKUP)
    .build();

// Trip update request
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
    .setName(tripName)
    .setTrip(trip)
    .setUpdateMask(FieldMask.newBuilder().addPaths("trip_status"))
    .build();

// Error handling.
try {
  Trip updatedTrip = tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case NOT_FOUND:            // The trip doesn't exist.
      break;
    case FAILED_PRECONDITION:  // The given trip status is invalid.
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}