字段掩码是一种供 API 调用方列出请求应检索或更新的字段的方式。使用 FieldMask 可避免不必要的工作并提高性能。Fleet Engine 使用字段掩码来更新所有资源中的字段。
使用车辆 ID 更新行程
您必须配置包含车辆 ID 的行程,以便 Fleet Engine 能够跟踪车辆的行驶路线。以下代码示例演示了如何使用车辆 ID 更新行程。
staticfinalStringPROJECT_ID="my-rideshare-co-gcp-project";staticfinalStringTRIP_ID="trip-8241890";StringtripName="providers/"+PROJECT_ID+"/trips/"+TRIP_ID;TripServiceBlockingStubtripService=TripService.newBlockingStub(channel);// The trip settings to update.Triptrip=Trip.newBuilder().setVehicleId("8241890").build();// The trip update request.UpdateTripRequestupdateTripRequest=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{TripupdatedTrip=tripService.updateTrip(updateTripRequest);}catch(StatusRuntimeExceptione){Statuss=e.getStatus();switch(s.getCode()){caseNOT_FOUND:// Neither the trip nor vehicle exist.break;casePERMISSION_DENIED:break;}return;}
staticfinalStringPROJECT_ID="my-rideshare-co-gcp-project";staticfinalStringTRIP_ID="trip-8241890";StringtripName="providers/"+PROJECT_ID+"/trips/"+TRIP_ID;TripServiceBlockingStubtripService=TripService.newBlockingStub(channel);// Trip settings to be updated.Triptrip=Trip.newBuilder().setTripStatus(TripStatus.ARRIVED_AT_PICKUP).build();// Trip update requestUpdateTripRequestupdateTripRequest=UpdateTripRequest.newBuilder().setName(tripName).setTrip(trip).setUpdateMask(FieldMask.newBuilder().addPaths("trip_status")).build();// Error handling.try{TripupdatedTrip=tripService.updateTrip(updateTripRequest);}catch(StatusRuntimeExceptione){Statuss=e.getStatus();switch(s.getCode()){caseNOT_FOUND:// The trip doesn't exist.break;caseFAILED_PRECONDITION:// The given trip status is invalid.break;casePERMISSION_DENIED:break;}return;}
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eFleet Engine allows you to update trip details like vehicle assignment, status changes, and passenger information using gRPC or REST.\u003c/p\u003e\n"],["\u003cp\u003eWhen updating trips, utilize field masks to specify the fields to be modified, enhancing performance and efficiency.\u003c/p\u003e\n"],["\u003cp\u003eYou manage a trip's state by updating its \u003ccode\u003eTripStatus\u003c/code\u003e field, reflecting the trip's progress from \u003ccode\u003eNEW\u003c/code\u003e to \u003ccode\u003eCOMPLETE\u003c/code\u003e or \u003ccode\u003eCANCELED\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eFor trips with a \u003ccode\u003eDEADLINE_EXCEEDED\u003c/code\u003e error, retry \u003ccode\u003eCreateTrip\u003c/code\u003e with the same trip ID to determine the previous request's outcome.\u003c/p\u003e\n"]]],["To update a trip in Fleet Engine, use the `UpdateTrip()` method via gRPC or REST, utilizing an `UpdateTripRequest` message and the appropriate service account credentials. Updates can include assigning a vehicle, changing the trip status, or modifying other fields. Field masks are used to specify which fields to update, improving efficiency. The `vehicle_id` can only be set when the trip is new, and the trip's state is managed via `TripStatus` values. Error handling is essential, especially for `DEADLINE_EXCEEDED` cases.\n"],null,["This document describes how to update a trip and manage its state, which\ninvolves using a field mask to set relevant fields for a trip. It assumes you\nhave set up Fleet Engine as described in this site and are working with a\nvehicle assigned to a trip.\n\nTrip update basics\n\nYour system uses Fleet Engine to update a trip in the following situations:\n\n- When assigning a vehicle to a trip after it's created.\n- When the status of the trip changes; for example, when the vehicle passes through waypoints.\n- When you update trip fields, such as the number of passengers and the drop-off point.\n\nTo update a trip, send a request using either gRPC and REST.\n\n- `UpdateTrip()` method: [gRPC](/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1#maps.fleetengine.v1.TripService) or [REST](/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/providers.trips/update)\n- `UpdateTripRequest` message: [gRPC](/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1#updatetriprequest) only\n\nUse the appropriate credentials for the service account of your project as\ndescribed in [Fleet Engine: Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts).\n\nUpdate trip fields\n\nYou can update any of the trip fields described in [Trip fields](/maps/documentation/mobility/fleet-engine/journeys/trips/create-trip#trip-fields) in **Create\na single destination trip** . For example, after you create a trip, it's a common\npractice to first find a vehicle and then update the trip `vehicle_id` field to\nassociate it with the vehicle that will carry out the trip.\n| **Important:** You can only set the vehicle_id for a **NEW** trip. If you want to changes vehicle for a trip, you must set the trip status to `NEW` and then assign the different vehicle.\n\nUse field masks\n\n\u003cbr /\u003e\n\nField masks are a way for API callers to list the fields that a request should\nor update. Using a [FieldMask](https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask)\navoids unnecessary works and improves performance. Fleet Engine uses field masks\nfor updating fields across all resources.\n\nUpdate the trip with the vehicle ID\n\nYou must configure a trip with a vehicle ID so that the Fleet Engine can track\nthe vehicle along its route. The following code sample demonstrates how to\nupdate the trip with a vehicle ID. \n\n static final String PROJECT_ID = \"my-rideshare-co-gcp-project\";\n static final String TRIP_ID = \"trip-8241890\";\n\n String tripName = \"providers/\" + PROJECT_ID + \"/trips/\" + TRIP_ID;\n\n TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);\n\n // The trip settings to update.\n Trip trip = Trip.newBuilder()\n .setVehicleId(\"8241890\")\n .build();\n\n // The trip update request.\n UpdateTripRequest updateTripRequest =\n UpdateTripRequest.newBuilder() // No need for the header.\n .setName(tripName)\n .setTrip(trip)\n .setUpdateMask(FieldMask.newBuilder().addPaths(\"vehicle_id\"))\n .build();\n\n // Error handling.\n // If the Fleet Engine has both a trip and vehicle with IDs, and if the\n // credentials validate, then the service updates the trip.\n try {\n Trip updatedTrip = tripService.updateTrip(updateTripRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case NOT_FOUND: // Neither the trip nor vehicle exist.\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nManage trip state for trips\n\nYou specify the state of a trip using one of the [`TripStatus`](/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1#tripstatus) enumeration\nvalues. When a trip's state changes; for example from `ENROUTE_TO_PICKUP` to\n`ARRIVED_AT_PICKUP`, you update the trip state in Fleet Engine. The trip\nlifecycle always begins with a state value of `NEW`, and ends with a value of\neither `COMPLETE` or `CANCELED`.\n\nExample trip update\n\nThe following demonstrates how to update the trip state for a back-to-back\ntrip in Fleet Engine. \n\n static final String PROJECT_ID = \"my-rideshare-co-gcp-project\";\n static final String TRIP_ID = \"trip-8241890\";\n\n String tripName = \"providers/\" + PROJECT_ID + \"/trips/\" + TRIP_ID;\n\n TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);\n\n // Trip settings to be updated.\n Trip trip = Trip.newBuilder()\n .setTripStatus(TripStatus.ARRIVED_AT_PICKUP)\n .build();\n\n // Trip update request\n UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()\n .setName(tripName)\n .setTrip(trip)\n .setUpdateMask(FieldMask.newBuilder().addPaths(\"trip_status\"))\n .build();\n\n // Error handling.\n try {\n Trip updatedTrip = tripService.updateTrip(updateTripRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case NOT_FOUND: // The trip doesn't exist.\n break;\n case FAILED_PRECONDITION: // The given trip status is invalid.\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nYou can see other examples of how to update trips in the **Other trip types**\nsection.\n\nHandle trip errors\n\nWhen updating or finding existing trips, you might encounter a case of a\n`DEADLINE_EXCEEDED` error, in which case the state of Fleet Engine is unknown.\nTo investigate this, first call `CreateTrip` again using the same trip ID you\nare trying to update or monitor. This should return either a 201 (CREATED) or\n409 (CONFLICT). In the latter case, the previous request succeeded before\n`DEADLINE_EXCEEDED`.\n\nSee the list of network errors in the Consumer SDK, either for [Android](/maps/documentation/mobility/journey-sharing/on-demand/android/share-journey#handle_consumer_sdk_errors) or\n[iOS](/maps/documentation/mobility/journey-sharing/on-demand/ios/share-journey#handle_consumer_sdk_errors).\n\nWhat's next\n\n- [Find trips](/maps/documentation/mobility/fleet-engine/journeys/trips/find-trip)"]]