Route Matrix

RouteMatrix 類別

class google.maps.routes.RouteMatrix
library "routes"

由 RouteMatrix.computeRouteMatrix 計算的一組起點/目的地配對路線矩陣

const {RouteMatrix} = await google.maps.importLibrary("routes");
computeRouteMatrix
computeRouteMatrix(request)
參數:
傳回值: Promise<{matrix:RouteMatrix}>
這項服務會接收起點和目的地清單,並傳回矩陣,其中包含每個起點和目的地組合的路線資訊。

注意:使用這個方法時,您必須設定 ComputeRouteMatrixRequest.fields 屬性,在要求中指定回應欄位遮罩。值為欄位路徑清單。

例如:
  • 所有可用欄位的欄位遮罩:fields: ['*']
  • 路線層級時間長度和距離的欄位遮罩:fields: ['durationMillis', 'distanceMeters']

不建議使用萬用字元回應欄位遮罩 fields: ['*'],原因如下:
  • 只選取所需欄位,可節省伺服器的運算週期,縮短傳回結果的延遲時間。
  • 在正式工作只選取所需欄位,可確保延遲時間穩定。我們日後可能會新增更多回應欄位,而這些新欄位可能需要額外的運算時間。如果選取所有欄位,或選取頂層的所有欄位,則可能會發生效能降低的情況,因為我們新增的任何新欄位都會自動包含在回應中。
  • 只選取所需欄位,可縮減回應大小,進而加快網路載入速度。
rows
readonly
矩陣的列。每一列都對應一個起點,並包含 RouteMatrixItem 陣列,每個陣列代表一條前往目的地的路線。
沿用: toJSON

RouteMatrixRow 類別

class google.maps.routes.RouteMatrixRow
library "routes"

對應至傳遞至 RouteMatrix.computeRouteMatrix 的來源。包含 RouteMatrixItem 清單,每個項目代表前往目的地的路線。

const {RouteMatrixRow} = await google.maps.importLibrary("routes");
items
readonly
每個目的地的路線資訊。
沿用: toJSON

RouteMatrixItem 類別

class google.maps.routes.RouteMatrixItem
library "routes"

包含為傳遞至 RouteMatrix.computeRouteMatrix 的起點/目的地組合計算的路線資訊。

const {RouteMatrixItem} = await google.maps.importLibrary("routes");
condition
readonly
類型: RouteMatrixItemCondition optional
指出是否找到路線。
distanceMeters
readonly
類型: number optional
路線的移動距離 (以公尺為單位)。
durationMillis
readonly
類型: number optional
沿著路線導航所需的時間長度 (以毫秒為單位)。如果將 ComputeRouteMatrixRequest.routingPreference 設為 TRAFFIC_UNAWARE,這個值會與 RouteMatrixItem.staticDurationMillis 相同。如果將 ComputeRouteMatrixRequest.routingPreference 設為 TRAFFIC_AWARE 或 TRAFFIC_AWARE_OPTIMAL,系統會考量路況來計算這個值。

注意:如果時間長度超過 2^53 毫秒,這個值會是 Number.POSITIVE_INFINITY。
error
readonly
類型: RouteMatrixItemError optional
計算路線時發生的錯誤。
fallbackInfo
readonly
類型: FallbackInfo optional
在某些情況下,如果伺服器無法根據指定偏好設定,計算特定起點/目的地組合的路線,可能會改用其他計算模式。使用備用廣告時,這個欄位會包含備用廣告回應的詳細資訊。否則這個欄位不會設定。
localizedValues
readonly
RouteMatrixItem 屬性的文字表示法。
staticDurationMillis
readonly
類型: number optional
不考慮路況時,沿路線行駛所需的時間。

注意:如果時間長度超過 2^53 毫秒,這個值會是 Number.POSITIVE_INFINITY。
travelAdvisory
readonly
類型: RouteTravelAdvisory optional
路線的其他資訊。

ComputeRouteMatrixRequest 介面

介面 google.maps.routes.ComputeRouteMatrixRequest

要求路線矩陣。

destinations
目的地陣列,決定回應矩陣的資料欄。以字串形式傳遞的值可以是地址、Plus Code 或地點資源名稱。系統不會考量海拔高度值。
fields
類型: Iterable<string>
要擷取的欄位集合。為所有欄位指定 ["*"]。
origins
起點陣列,決定回應矩陣的資料列。以字串形式傳遞的值可以是地址、Plus Code 或地點資源名稱。系統不會考量海拔高度值。

起點和目的地的基數有幾項大小限制:
arrivalTime
選填
類型: Date optional
抵達時間。

注意:只有在 ComputeRouteMatrixRequest.travelMode 設為 TRANSIT 時,才能設定這個值。您可以指定 ComputeRouteMatrixRequest.departureTime 或 ComputeRouteMatrixRequest.arrivalTime,但不能同時指定兩者。大眾運輸行程最多可顯示過去 7 天或未來 100 天的資訊。
departureTime
選填
類型: Date optional
出發時間。如未設定這個值,系統會預設為您提出要求的時間。

注意:只有在 ComputeRouteMatrixRequest.travelMode 設為 TRANSIT 時,才能指定過去的時間。大眾運輸行程最多可顯示過去 7 天或未來 100 天的資訊。
extraComputations
選填
可能用於完成要求的額外運算清單。

注意:這些額外運算可能會在回應中傳回額外欄位。您也必須在 ComputeRouteMatrixRequest.fields 中指定這些額外欄位,才能在回應中傳回。
internalUsageAttributionIds
選填
類型: Iterable<string> optional
用於將呼叫歸因於特定套件或 OSS 程式庫的 ID。
language
選填
類型: string optional
BCP-47 語言代碼,例如「en-US」或「sr-Latn」。詳情請參閱「Unicode 語言代碼 ID」。如需支援語言清單,請參閱「語言支援」。如未提供這個值,系統會根據 Google Maps JavaScript API 本地化設定推斷語言。否則,系統會根據第一個來源的位置推斷語言。
region
選填
類型: string optional
區碼,指定為 ccTLD (「頂層網域」) 的兩位字元值。詳情請參閱「國家/地區代碼頂層網域」。如未提供這個值,系統會根據 Google Maps JavaScript API 本地化設定推測區域。否則,系統會根據第一個來源的位置推斷區域。
routingPreference
選填
類型: RoutingPreference optional
指定如何計算路線矩陣。伺服器會嘗試使用所選路徑偏好設定,計算路徑矩陣。如果轉送偏好設定導致錯誤或延遲時間過長,系統會傳回錯誤。只有在 ComputeRouteMatrixRequest.travelMode 為 DRIVING 或 TWO_WHEELER 時,才能指定這個選項,否則要求會失敗。
trafficModel
選填
類型: TrafficModel optional
指定計算交通時間時要採用的假設。這項設定會影響 RouteMatrixItem.durationMillis 傳回的值,其中包含根據歷來平均值預估的交通時間。ComputeRouteMatrixRequest.trafficModel 僅適用於將 ComputeRouteMatrixRequest.routingPreference 設為 TRAFFIC_AWARE_OPTIMAL,並將 ComputeRouteMatrixRequest.travelMode 設為 DRIVING 的要求。預設值為 BEST_GUESS。
transitPreference
選填
類型: TransitPreference optional
指定偏好設定,影響 TRANSIT 路線傳回的路線。

注意:只有在 ComputeRouteMatrixRequest.travelMode 設為 TRANSIT 時,才能指定這個值。
travelMode
選填
類型: TravelMode optional
指定交通方式。
units
選填
類型: UnitSystem optional
指定顯示欄位的測量單位。如未提供這個值,系統會根據第一個起點的位置推論顯示單位。

ComputeRouteMatrixExtraComputation 常數

constants google.maps.routes.ComputeRouteMatrixExtraComputation
library "routes"

完成要求時要執行的額外運算。詳情請參閱網路服務說明文件。

這些常數也可以做為字串使用。在 TypeScript 中,字串常值是由 ComputeRouteMatrixExtraComputationString 類型定義。

const {ComputeRouteMatrixExtraComputation} = await google.maps.importLibrary("routes");
TOLLS 矩陣元素的過路費資訊。詳情請參閱網路服務說明文件。

RouteMatrixItemCondition 常數

constants google.maps.routes.RouteMatrixItemCondition
library "routes"

傳回路線的條件。詳情請參閱網路服務說明文件。

這些常數也可以做為字串使用。在 TypeScript 中,字串常值是由 RouteMatrixItemConditionString 類型定義。

const {RouteMatrixItemCondition} = await google.maps.importLibrary("routes");
ROUTE_EXISTS 系統找到路徑,並為元素填入相應資訊。詳情請參閱網路服務說明文件。
ROUTE_NOT_FOUND 找不到路線。詳情請參閱網路服務說明文件。

RouteMatrixItemError 類別

class google.maps.routes.RouteMatrixItemError extends Error
library "routes"

特定起點/目的地組合在RouteMatrix.computeRouteMatrix期間發生的錯誤。

const {RouteMatrixItemError} = await google.maps.importLibrary("routes");
沿用: toJSON

RouteMatrixItemLocalizedValues 類別

class google.maps.routes.RouteMatrixItemLocalizedValues
library "routes"

RouteMatrixItem 的文字表示法。

const {RouteMatrixItemLocalizedValues} = await google.maps.importLibrary("routes");
distance
readonly
類型: string optional
路徑矩陣項目的移動距離 (文字格式)。
distanceLanguage
readonly
類型: string optional
距離文字的 BCP-47 語言代碼,例如「en-US」或「sr-Latn」。

詳情請參閱 http://www.unicode.org/reports/tr35/#Unicode_locale_identifier。
duration
readonly
類型: string optional
時間長度,以文字形式表示,並根據查詢區域本地化。考量路況。

注意:如果您未要求路況資訊,這個值會與 RouteMatrixItemLocalizedValues.staticDuration 相同。
durationLanguage
readonly
類型: string optional
時間長度文字的 BCP-47 語言代碼,例如「en-US」或「sr-Latn」。

詳情請參閱 http://www.unicode.org/reports/tr35/#Unicode_locale_identifier。
staticDuration
readonly
類型: string optional
不考慮路況的車程時間,以文字形式表示。
staticDurationLanguage
readonly
類型: string optional
靜態時間長度文字的 BCP-47 語言代碼,例如「en-US」或「sr-Latn」。
transitFare
readonly
類型: string optional
大眾運輸票價,以文字形式表示。
transitFareLanguage
readonly
類型: string optional
大眾運輸票價文字的 BCP-47 語言代碼,例如「en-US」或「sr-Latn」。

詳情請參閱 http://www.unicode.org/reports/tr35/#Unicode_locale_identifier。
沿用: toJSON

RouteMatrixOrigin 介面

介面 google.maps.routes.RouteMatrixOrigin

ComputeRouteMatrixRequest 的單一來源。

waypoint
來源位置。以字串形式傳遞的值可以是地址或 Plus Code。系統不會考量海拔高度值。
routeModifiers
選填
類型: RouteModifiers optional
以此為起點的每條路線修飾符。