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

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

ข้อมูลเบื้องต้นเกี่ยวกับการสร้างการเดินทาง

ส่วนนี้จะอธิบายรายละเอียดคำขอที่จำเป็นสำหรับการสร้างการเดินทางใน Fleet Engine คุณส่งคำขอสร้างโดยใช้ gRPC และ REST

  • วิธี CreateTrip(): gRPC หรือ REST
  • ข้อความ CreateTripRequest: gRPC เท่านั้น

ช่องการเดินทาง

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

ฟิลด์การเดินทาง
ชื่อ จำเป็นหรือไม่ คำอธิบาย
parent ใช่ สตริงที่มีรหัสโปรเจ็กต์ รหัสนี้ต้องเป็นรหัสเดียวกับที่ใช้ในการผสานรวม Fleet Engine ทั้งหมด โดยมีบทบาทบัญชีบริการเดียวกัน
trip_id ใช่ สตริงที่คุณสร้างขึ้นซึ่งระบุการเดินทางนี้โดยไม่ซ้ำกัน รหัสการเดินทางมีข้อจำกัดบางอย่างตามที่ระบุไว้ในข้อมูลอ้างอิง
trip_type ใช่ ตั้งค่า TripType เป็นค่าต่อไปนี้สำหรับประเภทการเดินทางที่คุณกำลังสร้าง
  • ปลายทางเดียว: ตั้งค่าเป็น SHARED หรือ EXCLUSIVE
  • หลายปลายทาง: ตั้งค่าเป็น EXCLUSIVE
  • ต่อเนื่อง: ตั้งค่าเป็น EXCLUSIVE
  • การรวมพื้นที่เก็บข้อมูลร่วมกัน: ตั้งค่าเป็น SHARED
pickup_point ใช่ จุดเริ่มต้นของการเดินทาง
ปลายทางระหว่างทาง ใช่

การเดินทางแบบหลายจุดหมายเท่านั้น: รายการจุดหมายระหว่างทางที่คนขับแวะระหว่างการรับและส่ง เช่นเดียวกับ dropoff_point คุณสามารถตั้งค่าช่องนี้ในภายหลังได้โดยเรียกใช้ UpdateTrip แต่การเดินทางแบบหลายจุดหมายตามคำจำกัดความจะมีจุดหมายระหว่างทาง

vehicle_waypoints ใช่

การเดินทางแบบใช้รถร่วมกันเท่านั้น: ช่องนี้รองรับการสลับจุดอ้างอิงจากการเดินทางหลายครั้ง ซึ่งประกอบด้วยจุดอ้างอิงที่เหลือทั้งหมดสำหรับยานพาหนะที่กำหนด รวมถึงจุดอ้างอิงสำหรับการรับและส่งผู้โดยสารสำหรับการเดินทางนี้ คุณตั้งค่าช่องนี้ได้โดยเรียกใช้ CreateTrip หรือ UpdateTrip นอกจากนี้ คุณยังอัปเดตจุดอ้างอิงของยานพาหนะผ่านช่อง waypoints ด้วยการเรียกใช้ UpdateVehicle ได้ด้วย บริการจะไม่แสดงข้อมูลนี้ในการเรียก GetTrip เนื่องจากเหตุผลด้านความเป็นส่วนตัว

number_of_passengers ไม่ จำนวนผู้โดยสารในการเดินทาง
dropoff_point ไม่ จุดหมายของการเดินทาง
vehicle_id ไม่ รหัสของยานพาหนะที่กำหนดให้กับการเดินทาง

ตัวอย่าง: สร้างการเดินทางแบบใช้รถร่วมกัน

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

// 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;
}

อัปเดตการเดินทางแบบร่วมโดยสารที่แชร์

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

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

ตัวอย่างเช่น ลองพิจารณาการเดินทางแบบใช้รถร่วมกัน 2 รายการ ได้แก่ การเดินทาง A และการเดินทาง B ดังนี้

  • การเดินทาง A อยู่ระหว่างทางไปยังสถานที่ส่งผู้โดยสาร
  • จากนั้นระบบจะเพิ่มการเดินทาง B ไปยังยานพาหนะเดียวกัน

ใน UpdateTripRequest 1 รายการสำหรับการเดินทาง B คุณตั้งค่า vehicleId และตั้งค่า Trip.vehicle_waypoints เป็นลำดับจุดอ้างอิงที่เหมาะสมที่สุด ซึ่งก็คือจุดรับผู้โดยสาร B → จุดส่งผู้โดยสาร A → จุดส่งผู้โดยสาร B

  • การเรียก getVehicle() จะแสดงผล remainingWaypoints ที่มีข้อมูลต่อไปนี้
    B รับ → A ส่ง → B ส่ง
  • getTrip() หรือ Callback onTripRemainingWaypointsUpdated สำหรับการเดินทาง A จะแสดงผล remainingWaypoints ซึ่งมีข้อมูลต่อไปนี้
    จุดรับ B → จุดส่ง A
  • getTrip() หรือ Callback onTripRemainingWaypointsUpdated สำหรับการเดินทาง B จะแสดงผล remainingWaypoints ที่มี
    จุดรับผู้โดยสาร B → จุดส่งผู้โดยสาร A → จุดส่งผู้โดยสาร 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;
}

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