このチュートリアルでは、乗車と降車が 1 つのルートを作成し、その旅程をユーザーと共有するプロセスについて説明します。
前提条件
このチュートリアルを完了するには、次のことを完了してください。
Fleet Engine を設定する。詳細については、Fleet Engine: 初期設定をご覧ください。
アプリを Driver SDK と統合します。詳細については、Driver SDK の初期化(Android)と Driver SDK の統合ガイド(iOS)をご覧ください。
消費者向けアプリを Consumer SDK と統合します。詳細については、Android の場合は Consumer SDK のスタートガイド、iOS の場合は Consumer SDK のスタートガイドをご覧ください。
認証トークンを設定する。認証トークンの詳細については、Fleet Engine のスタートガイドの認可用の JSON ウェブトークンの作成と、Fleet Engine の Consumer SDK ドキュメントの認証と認可をご覧ください。
ステップ 1: Fleet Engine で車両を作成する
車両は、フリート内の車両を表すオブジェクトです。コンシューマ アプリで追跡できるようにするには、Fleet Engine で作成する必要があります。
車両を作成するには、次の 2 つの方法があります。
- gRPC
CreateVehicleRequest
リクエスト メッセージを指定してCreateVehicle()
メソッドを呼び出します。CreateVehicle()
を呼び出すには、Fleet Engine のスーパー ユーザー権限が必要です。- REST
https://fleetengine.googleapis.com/v1/providers.vehicles.create
を呼び出します。
注意点
車両を作成する際は、次の点に注意してください。
車両の初期状態は必ず
OFFLINE
に設定してください。これにより、Fleet Engine はルート マッチングに使用する車両を検出できるようになります。車両の
provider_id
は、Fleet Engine の呼び出しに使用されるサービス アカウントを含む Google Cloud プロジェクトのプロジェクト ID と同じである必要があります。複数のサービス アカウントが同じライドシェア プロバイダの Fleet Engine にアクセスできますが、Fleet Engine は現在、同じ車両にアクセスする異なる Google Cloud プロジェクトのサービス アカウントをサポートしていません。CreateVehicle()
から返されるレスポンスには、Vehicle
インスタンスが含まれます。UpdateVehicle()
を使用して更新されていない場合、インスタンスは 7 日後に削除されます。車両がまだ存在しないことを確認するため、CreateVehicle()
を呼び出す前にGetVehicle()
を呼び出す必要があります。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;
}
手順 2. 位置情報の記録を有効にする
位置情報の追跡とは、移動中の車両の位置を追跡することです。ドライバー アプリは、車両の現在地を含む Fleet Engine にテレメトリーを送信します。この常時更新される位置情報のストリームは、ルート上の車両の移動状況を伝えるために使用されます。位置情報の追跡を有効にすると、ドライバアプリはデフォルトの頻度(5 秒に 1 回)でこのテレメトリーの送信を開始します。
Android と iOS で位置情報の追跡を有効にする手順は次のとおりです。
Driver SDK for Android メソッド
enableLocationTracking()
を呼び出します。Driver SDK for iOS のブール値プロパティ
locationTrackingEnabled
をtrue
に設定します。
例
次のコード例は、位置情報の追跡を有効にする方法を示しています。
Java
RidesharingVehicleReporter vehicleReporter = ...;
vehicleReporter.enableLocationTracking();
Kotlin
val vehicleReporter = ...
vehicleReporter.enableLocationTracking()
Swift
vehicleReporter.locationTrackingEnabled = true
Objective-C
_vehicleReporter.locationTrackingEnabled = YES;
ステップ 3: 車両の状態をオンラインに設定する
車両の状態を online に設定することで車両を運行(つまり使用可能にする)できますが、これは位置情報の追跡を有効にするまで実行できません。
Android と iOS の車両の状態をオンラインに設定する方法は次のとおりです。
Driver SDK for Android メソッド
setVehicleState(VehicleState.ONLINE)
を呼び出します。Driver SDK for iOS のメソッド
vehicleReporter.update(.online)
を呼び出します。
例
次のコード例は、車両の状態を ONLINE
に設定する方法を示しています。
Java
vehicleReporter.setVehicleState(VehicleState.ONLINE);
Kotlin
vehicleReporter.setVehicleState(VehicleState.ONLINE)
Swift
vehicleReporter.update(.online)
Objective-C
[_vehicleReporter updateVehicleState:GMTDVehicleStateOnline];
ステップ 4: Fleet Engine でルートを作成する
プログラムでは、Trip
は行程を表すオブジェクトとなります。車両と照合して追跡できるように、ルート リクエストごとにオブジェクトを作成する必要があります。
- ルートを作成するには、
CreateTripRequest
リクエスト メッセージを指定してCreateTrip()
メソッドを呼び出します。
必須属性
ルートを作成するには、以下のフィールドが必要です。
parent
- プロバイダ ID を含む文字列。これは、Fleet Engine の呼び出しに使用されるサービス アカウントを含む Google Cloud プロジェクトのプロジェクト ID と同じである必要があります。
trip_id
- このルートを一意に識別する文字列。ご自身で作成します。
trip_type
TripType
列挙値のいずれか(SHARED
またはEXCLUSIVE
)。pickup_point
- ルートの出発地。
ルートを作成する際に number_of_passengers
、dropoff_point
、vehicle_id
を指定できますが、これらのフィールドは必須ではありません。vehicle_id
を指定すると、ルートに残りのウェイポイントのリストが含まれ、それを使用してドライバー アプリで目的地を設定できます。
例
次の例は、グランド インドネシア イースト モールへのルートを作成する方法を示しています。乗客 2 名が関与する 1 行は排他的で、ステータスが new です。ルートの provider_id
はプロジェクト ID と同じにする必要があります。この例では、配車サービス プロバイダがプロジェクト ID my-rideshare-co-gcp-project
で Google Cloud プロジェクトを作成しました。このプロジェクトには、Fleet Engine を呼び出すためのサービス アカウントを含める必要があります。
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// Trip initial settings.
String parent = "providers/" + PROJECT_ID;
Trip trip = Trip.newBuilder()
.setTripType(TripType.EXCLUSIVE) // Use TripType.SHARED for carpooling.
.setPickupPoint( // Grand Indonesia East Mall.
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.195139).setLongitude(106.820826)))
.setNumberOfPassengers(2)
// Provide the drop-off point if available.
.setDropoffPoint(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.1275).setLongitude(106.6537)))
.build();
// Create trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
.setParent(parent)
.setTripId("trip-8241890") // Trip ID assigned by the provider.
.setTrip(trip) // The initial state is NEW.
.build();
// Error handling.
try {
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. ドライバー アプリで目的地を設定する
ユーザーとドライバーをペア設定したら、ドライバー アプリでルートの目的地を設定する必要があります。車両の目的地は、GetTrip()
、UpdateTrip()
、GetVehicle()
によって返されるウェイポイント コレクションから取得できます。
- デスティネーションを設定するには、Navigation SDK for Android のメソッド
setDestination()
を呼び出すか、Navigation SDK for iOS のメソッドsetDestinations()
を呼び出します。
ユーザーアプリがルートを適切にレンダリングするには、setDestination()
に渡される地理座標(LatLng)がルートの地点の座標と一致している必要があります。詳細については、単一の宛先へのルーティングと複数の宛先へのルーティングのチュートリアルをご覧ください。
例
次のコードサンプルは、ドライバアプリでデスティネーションを設定する方法を示しています。
Java
private void navigateToPlace(String placeId, RoutingOptions travelMode) {
Waypoint destination;
try {
destination = Waypoint.fromPlaceId(placeId, null);
} catch (Waypoint.UnsupportedPlaceIdException e) {
displayMessage("Error starting navigation: Place ID is not supported.");
return;
}
// Create a future to await the result of the asynchronous navigator task.
ListenableResultFuture<Navigator.RouteStatus> pendingRoute =
mNavigator.setDestination(destination, travelMode);
// Define the action to perform when the SDK has determined the route.
pendingRoute.setOnResultListener(
new ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() {
@Override
public void onResult(Navigator.RouteStatus code) {
switch (code) {
case OK:
// Hide the toolbar to maximize the navigation UI.
if (getActionBar() != null) {
getActionBar().hide();
}
// Enable voice audio guidance (through the device speaker).
mNavigator.setAudioGuidance(
Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE);
// Simulate vehicle progress along the route for demo/debug builds.
if (BuildConfig.DEBUG) {
mNavigator.getSimulator().simulateLocationsAlongExistingRoute(
new SimulationOptions().speedMultiplier(5));
}
// Start turn-by-turn guidance along the current route.
mNavigator.startGuidance();
break;
// Handle error conditions returned by the navigator.
case NO_ROUTE_FOUND:
displayMessage("Error starting navigation: No route found.");
break;
case NETWORK_ERROR:
displayMessage("Error starting navigation: Network error.");
break;
case ROUTE_CANCELED:
displayMessage("Error starting navigation: Route canceled.");
break;
default:
displayMessage("Error starting navigation: "
+ String.valueOf(code));
}
}
});
}
Kotlin
private fun navigateToPlace(placeId: String, travelMode: RoutingOptions) {
val destination =
try {
Waypoint.fromPlaceId(placeId, null)
} catch (e: Waypoint.UnsupportedPlaceIdException) {
displayMessage("Error starting navigation: Place ID is not supported.")
return@navigateToPlace
}
// Create a future to await the result of the asynchronous navigator task.
val pendingRoute = mNavigator.setDestination(destination, travelMode)
// Define the action to perform when the SDK has determined the route.
pendingRoute.setOnResultListener(
object : ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() {
override fun onResult(code: Navigator.RouteStatus) {
when (code) {
Navigator.RouteStatus.OK -> {
// Hide the toolbar to maximize the navigation UI.
getActionBar()?.hide()
// Enable voice audio guidance (through the device speaker).
mNavigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
// Simulate vehicle progress along the route for demo/debug builds.
if (BuildConfig.DEBUG) {
mNavigator
.getSimulator()
.simulateLocationsAlongExistingRoute(SimulationOptions().speedMultiplier(5))
}
// Start turn-by-turn guidance along the current route.
mNavigator.startGuidance()
}
Navigator.RouteStatus.NO_ROUTE_FOUND -> {
displayMessage("Error starting navigation: No route found.")
}
Navigator.RouteStatus.NETWORK_ERROR -> {
displayMessage("Error starting navigation: Network error.")
}
Navigator.RouteStatus.ROUTE_CANCELED -> {
displayMessage("Error starting navigation: Route canceled.")
}
else -> {
displayMessage("Error starting navigation: ${code.name}")
}
}
}
}
)
}
Swift
private func startNavigation() {
let destinations = [
GMSNavigationWaypoint(
placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk", title: "PCC Natural Market"),
GMSNavigationWaypoint(
placeID: "ChIJJ326ROcSkFQRBfUzOL2DSbo", title: "Marina Park"),
]
mapView.navigator?.setDestinations(destinations, callback: { routeStatus in
guard routeStatus == .OK else {
// Error starting navigation.
return
}
mapView.locationSimulator?.simulateLocationsAlongExistingRoute()
mapView.navigator?.isGuidanceActive = true
mapView.navigator?.sendsBackgroundNotifications = true
mapView.cameraMode = .following
})
}
Objective-C
- (void)startNavigation {
NSArray<GMSNavigationWaypoint *> *destinations =
@[[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk"
title:@"PCC Natural Market"],
[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo"
title:@"Marina Park"]];
[_mapView.navigator setDestinations:destinations
callback:^(GMSRouteStatus routeStatus) {
if (routeStatus != GMSRouteStatusOK) {
// Error starting navigation.
return;
}
[_mapView.locationSimulator simulateLocationsAlongExistingRoute];
_mapView.navigator.guidanceActive = YES;
_mapView.navigator.sendsBackgroundNotifications = YES;
_mapView.cameraMode = GMSNavigationCameraModeFollowing;
}];
}
ステップ 6. ユーザーアプリでルートの更新情報をリッスンする
Android では、
TripModelManager
からTripModel
オブジェクトを取得してTripModelCallback
リスナーを登録することで、ルートからのデータ更新をリッスンできます。iOS の場合、
GMTCTripService
からGMTCTripModel
オブジェクトを取得してGMTCTripModelSubscriber
サブスクライバーを登録することで、ルートからのデータ更新をリッスンできます。
TripModelCallback
リスナーと GMTCTripModelSubscriber
サブスクライバーにより、アプリは自動更新間隔に基づいて更新のたびに移動状況の更新を定期的に受信できます。変化した値のみがコールバックをトリガーできます。それ以外の場合、コールバックは通知されません。
TripModelCallback.onTripUpdated()
メソッドと tripModel(_:didUpdate:updatedPropertyFields:)
メソッドは、データが変更されたかどうかに関係なく常に呼び出されます。
例 1
次のコードサンプルは、TripModelManager
/GMTCTripService
から TripModel
を取得してリスナーを設定する方法を示しています。
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 for Android メソッド
TripModel.getTripInfo()
を呼び出します。このメソッドを呼び出してもデータの更新は強制されませんが、データは更新頻度で更新され続けます。Consumer SDK for iOS のプロパティ
GMTCTripModel.currentTrip
を取得します。
ステップ 7. 車両 ID でルートを更新してください
Fleet Engine が経路に沿って車両を追跡できるように、ルートに車両 ID を設定する必要があります。
- 車両 ID でルートを更新するには、
UpdateTripRequest
を使用してUpdateTrip
エンドポイントを呼び出します。update_mask
フィールドを使用して、車両 ID を更新することを指定します。
メモ
ルートの作成時に目的地を指定しなかった場合は、ここでいつでも指定できます。
進行中のルートの車両を変更する必要がある場合は、ルートのステータスを新しい状態に戻し、(上記の手順で)新しい車両 ID でルートを更新する必要があります。
例
次のコードサンプルでは、車両 ID を指定してルートを更新する方法を示しています。
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);
// The trip settings to update.
Trip trip = Trip.newBuilder()
.setVehicleId("8241890")
.build();
// The trip update request.
UpdateTripRequest updateTripRequest =
UpdateTripRequest.newBuilder() // No need for the header.
.setName(tripName)
.setTrip(trip)
.setUpdateMask(FieldMask.newBuilder().addPaths("vehicle_id"))
.build();
// Error handling.
// If the Fleet Engine has both a trip and vehicle with IDs, and if the
// credentials validate, 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: // Neither the trip nor vehicle exist.
break;
case PERMISSION_DENIED:
break;
}
return;
}
ステップ 8. ユーザー アプリにジャーニーを表示する
ConsumerController
オブジェクトを使用して、配車と配達のユーザー インターフェース要素の API にアクセスします。
詳しくは、User Interface Element 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];
ステップ 9. 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;
}