建構要求訊息
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如「路線最佳化總覽」簡要說明,基本要求包含 Model、Shipments 和 Vehicles 這幾項必要實體:
- 模型會擷取整個要求的設定和限制,包括
Shipments
和 Vehicles
。
- 貨運代表包含取貨和送貨
VisitRequest
的工作或實際貨運。出貨內容有當地設定和限制。
- 車輛代表車輛、駕駛人或人員。車輛也有當地設定和限制。
每個實體的屬性都會描述特定精細程度的某部分最佳化問題。模型層級的限制會套用至所有貨運和車輛,而貨運或車輛上指定的限制和屬性則僅適用於單一貨運或車輛。
如需各訊息類型的完整說明文件,請參閱 ShipmentModel
(REST、gRPC)、Shipment
(REST、gRPC) 和 Vehicle
(REST、gRPC) 訊息的參考說明文件。
OptimizeToursRequest
個房源
頂層 OptimizeToursRequest
訊息 (REST、gRPC) 的常用屬性包括:
searchMode
表示是否要傳回符合指定限制的第一個解決方案,或是在設定的期限內找出最佳解決方案。
considerRoadTraffic
會決定是否使用即時路況來規劃路線和預估抵達時間。
populateTransitionPolylines
會決定是否在回應中傳回路線折線和路線權杖。
模型屬性
ShipmentModel
訊息 (REST、gRPC) 的常見屬性包括:
globalStartTime
代表所有車輛和貨運路線的最早開始時間。在此時間之前,車輛不得開始首次轉運和出貨。
globalEndTime
代表所有車輛和貨運路線的最新結束時間。所有指派的貨運和轉移作業都必須在此時間前完成。
出貨屬性
Shipment
訊息 (REST、gRPC) 的常見屬性包括:
pickups[]
和 deliveries[]
代表可取貨或送貨的地點。pickups[]
和 deliveries[]
屬性都使用 VisitRequest
訊息 (REST、gRPC)。
loadDemands
代表車輛完成運送作業所需的負載。車輛的對應 load_limits
(REST、gRPC) 屬性代表車輛一次可容納的負載量。如要進一步瞭解負載,請參閱「負載需求和限制」。
penalty_cost
代表跳過運送時產生的費用。如要進一步瞭解費用,請參閱「費用模型參數」。
車輛屬性
Vehicle
訊息 (REST、gRPC) 的常見屬性包括:
startLocation
代表車輛必須開始路線的位置。這是選用屬性。如未指定,車輛路線會從第一個指派貨件的位置開始。
endLocation
代表車輛必須結束路線的位置。這是選用屬性。如未指定,車輛路線會在最後指派的貨物位置結束。
startTimeWindows[]
代表車輛可以開始行駛路線的時間。這是選用屬性。
endTimeWindows[]
代表車輛可以開始和結束路線的時間。
這兩項屬性都是選用屬性。
loadLimits
代表車輛可滿足貨物裝載需求的容量。如要進一步瞭解負載需求和限制,請參閱「負載需求和限制」。
JSON 格式的完整要求範例如下:
{
"model": {
"shipments": [
{
"pickups": [
{
"arrivalLocation": {
"latitude": 37.73881799999999,
"longitude": -122.4161
}
}
],
"deliveries": [
{
"arrivalLocation": {
"latitude": 37.79581,
"longitude": -122.4218856
}
}
]
}
],
"vehicles": [
{
"startLocation": {
"latitude": 37.73881799999999,
"longitude": -122.4161
},
"endLocation": {
"latitude": 37.73881799999999,
"longitude": -122.4161
},
"costPerKilometer": 1.0
}
],
"globalStartTime": "2024-02-13T00:00:00.000Z",
"globalEndTime": "2024-02-14T06:00:00.000Z"
}
}
OptimizeTours
和 BatchOptimizeTours
都會像上述範例一樣取用要求訊息,但方式不同。提出路線最佳化要求前,請務必瞭解這兩種方法的差異:
比較 OptimizeTours 和 BatchOptimizeTours
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eRoute Optimization requests require defining a \u003ccode\u003eModel\u003c/code\u003e with \u003ccode\u003eShipments\u003c/code\u003e and \u003ccode\u003eVehicles\u003c/code\u003e to represent the optimization problem.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eModel\u003c/code\u003e sets global parameters, while \u003ccode\u003eShipments\u003c/code\u003e and \u003ccode\u003eVehicles\u003c/code\u003e have individual properties and constraints for pickups, deliveries, loads, and time windows.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eOptimizeToursRequest\u003c/code\u003e allows controlling the search mode, traffic considerations, and response details.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eOptimizeTours\u003c/code\u003e and \u003ccode\u003eBatchOptimizeTours\u003c/code\u003e offer synchronous and asynchronous request processing, respectively, with key differences in handling large or complex scenarios.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify global start and end times for all vehicles and shipments within the \u003ccode\u003eShipmentModel\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Construct a request message\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nAs briefly described in [Route Optimization Overview](/maps/documentation/route-optimization/overview), a basic request\nconsists of **Model** , **Shipments** , and **Vehicles** as required entities:\n\n- **Model** captures settings and constraints for the entire request, inclusive of both `Shipments` and `Vehicles`.\n- **Shipments** represent tasks or actual shipments that include pickup and delivery `VisitRequest`s. Shipments have local settings and constraints.\n- **Vehicles** represent vehicles, drivers, or personnel. Vehicles also have local settings and constraints.\n\nEach entity's properties describe part of an optimization problem at a\nparticular level of granularity. Model-wide constraints are applied to all\nshipments and vehicles, while constraints and properties specified on shipments\nor vehicles are specific to a single shipment or vehicle.\n\nFor complete documentation on each message type, see the reference documentation\nfor `ShipmentModel` ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.ShipmentModel)), `Shipment` ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#Shipment), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Shipment)),\nand `Vehicle` ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#Vehicle), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Vehicle)) messages.\n\n`OptimizeToursRequest` properties\n---------------------------------\n\nSome commonly used properties of the top-level `OptimizeToursRequest` message\n([REST](/maps/documentation/route-optimization/reference/rest/v1/projects/optimizeTours), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.OptimizeToursRequest)) include the following:\n\n- `searchMode` indicates whether to return the first solution that satisfies specified constraints or find the best possible solution within a set deadline.\n- `considerRoadTraffic` determines whether or not live traffic is used for routing and ETA estimation.\n- `populateTransitionPolylines` determines whether or not route polylines and route tokens are returned in the response.\n\nModel properties\n----------------\n\nSome commonly used properties of the `ShipmentModel` message ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel),\n[gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.ShipmentModel)) include:\n\n- `globalStartTime` represents the earliest start time of routes across all vehicles and shipments. No vehicle may start its first transitions and shipments before this time.\n- `globalEndTime` represents the latest end time of routes across all vehicles and shipments. All assigned shipments and transitions must be complete before this time.\n\nShipment properties\n-------------------\n\nSome commonly used properties of the `Shipment` message ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#Shipment), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Shipment))\ninclude:\n\n- `pickups[]` and `deliveries[]` represent where a shipment can be picked up or dropped off. `pickups[]` and `deliveries[]` properties both use the `VisitRequest` message ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#VisitRequest), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Shipment.VisitRequest)).\n- `loadDemands` represent the load required for a vehicle to complete a shipment. Vehicles' corresponding `load_limits` ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#Vehicle.FIELDS.load_limits), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Vehicle.FIELDS.repeated.google.maps.routeoptimization.v1.Vehicle.LoadLimitsEntry.google.maps.routeoptimization.v1.Vehicle.load_limits)) property represents how much load a vehicle can accommodate at one time. Read more about load in [Load Demands and Limits](/maps/documentation/route-optimization/load-demands-limits).\n- `penalty_cost` represents the cost incurred if a shipment is skipped. Read more on costs in [Cost Model Parameters](/maps/documentation/route-optimization/cost-model).\n\nVehicle properties\n------------------\n\nSome commonly used properties of the `Vehicle` message ([REST](/maps/documentation/route-optimization/reference/rest/v1/ShipmentModel#Vehicle), [gRPC](/maps/documentation/route-optimization/reference/rpc/google.maps.routeoptimization.v1#google.maps.routeoptimization.v1.Vehicle))\ninclude:\n\n- `startLocation` represents where a vehicle must start its route. This property is optional. If not specified, the vehicle's route starts at the location of its first assigned shipment.\n- `endLocation` represents where a vehicle must end its route. This property is optional. If not specified, the vehicle's route ends at the location of its last assigned shipment.\n- `startTimeWindows[]` represents when a vehicle can start its route. This property is optional.\n- `endTimeWindows[]` represents when a vehicle can start and end its route. Both properties are optional.\n- `loadLimits` represent the vehicle's capacity available to meet shipments' load demands. Read more about load demands and limits in [Load Demands and\n Limits](/maps/documentation/route-optimization/load-demands-limits).\n\nA complete example request in JSON format looks like: \n\n {\n \"model\": {\n \"shipments\": [\n {\n \"pickups\": [\n {\n \"arrivalLocation\": {\n \"latitude\": 37.73881799999999,\n \"longitude\": -122.4161\n }\n }\n ],\n \"deliveries\": [\n {\n \"arrivalLocation\": {\n \"latitude\": 37.79581,\n \"longitude\": -122.4218856\n }\n }\n ]\n }\n ],\n \"vehicles\": [\n {\n \"startLocation\": {\n \"latitude\": 37.73881799999999,\n \"longitude\": -122.4161\n },\n \"endLocation\": {\n \"latitude\": 37.73881799999999,\n \"longitude\": -122.4161\n },\n \"costPerKilometer\": 1.0\n }\n ],\n \"globalStartTime\": \"2024-02-13T00:00:00.000Z\",\n \"globalEndTime\": \"2024-02-14T06:00:00.000Z\"\n }\n }\n\n`OptimizeTours` and `BatchOptimizeTours` both consume request messages like the\nexample above, but in different ways. Before making a Route Optimization\nrequest, it's important to understand the difference between the two methods:\n\n[Comparing OptimizeTours and BatchOptimizeTours](/maps/documentation/route-optimization/sync-vs-async)"]]