根據預設,中途停靠點用於停靠取貨和送貨,但您也可以指定中途停靠點僅供通過。
如果路線包含起點路線控點、通過式中途路線控點和目的地路線控點,則只會包含一個連接起點和目的地的路線路段,同時通過中途 (稱為 via
) 路線控點。
如要將中途停靠點設為直通停靠點,請使用 Waypoint (REST) 或 Waypoint (gRPC),將停靠點的 via
屬性設為 true
。
使用者在地圖上拖曳途中的航點時,最適合使用 via
屬性建立路線。這樣一來,使用者就能即時查看最終路線,並確保航點放置在 Compute Routes 可存取的位置。
以下範例說明如何將中途停靠點標示為直通停靠點。
{ "location": { "latLng": { "latitude":37.419734, "longitude":-122.0827784 } }, "via": true }
存取中途停靠點的地點 ID
如果您將起點、目的地或中繼路線控點的位置指定為地址字串或 Plus Code,API 會嘗試找出具有相應地點 ID 的最相關位置。結果中的 geocodingResults.intermediates
陣列包含對應至路徑點位置的地點 ID,以及地點的其他資料。
針對 intermediates
陣列的每個元素,使用 intermediateWaypointRequestIndex
屬性判斷要求中的哪個中途停靠點對應於回應中的地點 ID。
例如:
您可以在要求中指定三個中途航點。其中兩個中途點由地址字串指定,一個由經緯度座標指定。
在回應欄位遮罩中加入
geocodingResults
,即可指定要在結果中傳回geocodingResults
陣列。
curl -X POST -d '{ "origin":{ "address": "1600 Amphitheatre Parkway, Mountain View, CA" }, "destination":{ "address": "24 Willie Mays Plaza, San Francisco, CA 94107" }, "intermediates": [ { "address": "450 Serra Mall, Stanford, CA 94305, USA" }, { "location":{ "latLng":{ "latitude": 37.419734, "longitude": -122.0807784 } } }, { "address": "1836 El Camino Real, Redwood City, CA 94063" } ], "travelMode": "DRIVE", }' -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' -H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.legs,geocodingResults' 'https://routes.googleapis.com/directions/v2:computeRoutes'
回覆包含 geocodingResults
陣列,其中含有起點、目的地和其中兩個路線控點的地點 ID。這些是要求中索引 0 和 2 的路徑點。由於要求中索引 1 的中途點是使用經緯度座標指定,因此回應中的 geocodingResults
陣列會省略該中途點。
{ "routes": [{...}], "geocodingResults": { "origin": { "geocoderStatus": {}, "type": [ "premise" ], "placeId": "ChIJj38IfwK6j4ARNcyPDnEGa9g" }, "destination": { "geocoderStatus": {}, "type": [ "premise" ], "placeId": "ChIJI7ES6tl_j4ARVpDZoXbWAiY" }, "intermediates": [ { "geocoderStatus": {}, "intermediateWaypointRequestIndex": 0, "type": [ "street_address" ], "placeId": "ChIJvdLMGyq7j4ARScE5tWX_C0Y" }, { "geocoderStatus": {}, "intermediateWaypointRequestIndex": 2, "type": [ "premise" ], "placeId": "ChIJkTc0GKajj4AR9UMsOSHwGD0" } ] } }