現在、
Routes Preferred API は一部のお客様のみご利用いただけます。詳しくは、
営業担当者までお問い合わせください。
ルート マトリックスを計算する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Routes Preferred API の ComputeRouteMatrix
メソッドを使用すると、複数の出発地と目的地のルートの距離と所要時間を計算できます。ComputeRouteMatrix
は、ストリーミング gRPC 呼び出しと REST HTTP 呼び出しの両方をサポートしています。
出発地と目的地のペアのリストを指定すると、ComputeRouteMatrix
は各出発地から各目的地までのルートの距離と所要時間を計算します。ストリームの各要素は、単一のルートの情報に対応します。
ComputeRouteMatrix
メソッドには、距離行列サービスに比べて次のようなメリットがあります。
- ストリーミングにより、行列全体が計算される前に要素を返すことができるため、レイテンシが短縮されます。
ComputeRouteMatrix
には、交通量の計算に関するきめ細かいオプションがあり、品質とレイテンシのトレードオフを決定できます。
- リクエストは Google のインフラストラクチャ内で高い優先度で実行されるため、可用性が向上します。
- 経由地の見出し(進行方向)と道路の通行側の情報を指定できます。
- ルートの距離や到着予定時刻とともに、有料道路の情報を返すようリクエストできます。
ComputeRouteMatrix メソッドを有効にする
コードで ComputeRouteMatrix
メソッドを使用する前に、このメソッドを有効にする必要があります。Routes Preferred API メソッドの有効化の詳細については、スタートガイドをご覧ください。
ComputeRouteMatrix メソッドを使用する
ComputeRouteMatrix
メソッドは、Routes Preferred v1 エンドポイントで使用できます。
詳細については、次のドキュメントをご覧ください。
gRPC:
https://developers.google.com/maps/documentation/routes_preferred/reference/rpc/google.maps.routes.v1
REST:
https://developers.google.com/maps/documentation/routes_preferred/reference/rest/v1/TopLevel/computeRouteMatrix
次の一般的な手順では、gRPC を使用して ComputeRouteMatrix
メソッドを呼び出す方法について説明します。
googleapis リポジトリから必要な protobuffer をチェックアウトします。
https://github.com/googleapis/googleapis/tree/master/google/maps/routes
Go などの一部のビルドシステムでは、この処理が自動的に行われる場合があります。
使用する言語とビルドツールのセットを使用して、関連するプロトバッファ コードを生成します。
リクエストを生成します。リクエストとともに、次の 2 つの必須メタデータを送信する必要があります。
- X-Goog-Api-Key は API キーに設定する必要があります。
X-Goog-Fieldmask は、レスポンスで必要なフィールドのカンマ区切りのリストに設定する必要があります。たとえば、X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status です。ワイルドカード文字(*)を使用してすべてのフィールドを指定できますが、これはおすすめしません。ワイルドカード文字を使用して API レスポンスをテストして理解し、本番環境のコードのフィールド マスクで必要なフィールドを明示的にリストします。
フィールド マスク文字列の作成方法の詳細については、https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto をご覧ください。不要なフィールドを無効にすると、それらのフィールドで追加の計算が必要なくなるため、レイテンシを短縮できます。必要なフィールドを指定し、後でより多くの計算を必要とする新しいフィールドを追加すると、レイテンシは安定した状態を維持します。
リクエストを routespreferred.googleapis.com:443
に送信します。TLS プロトコルを使用する必要があります。
ComputeRouteMatrix
メソッドの使用例については、ルート マトリックスの計算(ベータ版)の例をご覧ください。
ComputeRouteMatrix
メソッドの大きな特徴の 1 つは、エラーコードをストリーム全体または個々の要素に対して返すことができることです。たとえば、リクエストの形式が正しくない場合(オリジンがゼロの場合など)、ストリーム接続はエラーを返します。ただし、エラーがストリームのいくつかの要素にのみ適用される場合(たとえば、配信元に無効なプレイス ID を設定した場合)、エラーの影響を受ける要素にのみエラーコードが含まれます。
ストリームから返される要素は、特定の順序で返されるとは限りません。このため、各要素には origin_index
と destination_index
が含まれます。リクエストで指定された出発地と目的地のルートの出発地は、特定の要素に対して origins[origin_index]
と同等であり、ルートの目的地は destinations[destination_index]
と同等です。これらの配列は 0 から始まります。送信元リストと宛先リストの順序を保存することが重要です。
通行料金の計算
通行料金の計算については、通行料金を計算するをご覧ください。
通行料金の計算例については、ルート マトリックスの計算例をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-22 UTC。
[null,null,["最終更新日 2025-07-22 UTC。"],[[["\u003cp\u003eThe \u003ccode\u003eComputeRouteMatrix\u003c/code\u003e method in the Routes Preferred API calculates distances and durations for multiple origin-destination pairs, offering advantages like streaming results and traffic calculation options.\u003c/p\u003e\n"],["\u003cp\u003eIt's necessary to enable the \u003ccode\u003eComputeRouteMatrix\u003c/code\u003e method and understand quota limits before implementation, ensuring compliance with Google Maps Platform terms.\u003c/p\u003e\n"],["\u003cp\u003eThe method can be accessed through gRPC or REST, with specific steps outlined for using gRPC, including generating protobuffer code and configuring requests.\u003c/p\u003e\n"],["\u003cp\u003eResponses include \u003ccode\u003eorigin_index\u003c/code\u003e and \u003ccode\u003edestination_index\u003c/code\u003e to correlate results with input data since the order of elements is not guaranteed.\u003c/p\u003e\n"],["\u003cp\u003eToll fee calculation functionality is available within \u003ccode\u003eComputeRouteMatrix\u003c/code\u003e, allowing developers to incorporate toll costs into route planning.\u003c/p\u003e\n"]]],["The `ComputeRouteMatrix` method of the Routes Preferred API calculates route distance and duration for multiple origins and destinations via streaming gRPC or REST HTTP calls. It offers lower latency, fine-grained traffic options, higher availability, waypoint specifications, and toll information. To use it, enable the method, and send requests with API key and field mask. The method returns elements with `origin_index` and `destination_index`. Error codes can be returned for the entire stream or per individual element.\n"],null,["# Compute a Route Matrix\n\n\u003cbr /\u003e\n\n| This product or feature is in Preview (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage\n| descriptions](/maps/launch-stages).\n\n\u003cbr /\u003e\n\nYou can calculate the distance and duration of a route for multiple\norigins and destinations by using the `ComputeRouteMatrix` method\nof the Routes Preferred API. `ComputeRouteMatrix` supports\nboth streaming gRPC calls and REST HTTP calls.\n\nGiven a list of origin and destination pairs, `ComputeRouteMatrix`\ncalculates the distance and duration of a route starting at each origin\nand ending at each destination. Each element of the stream corresponds\nto the information for a single route.\n\nThe `ComputeRouteMatrix` method has several advantages over the\nDistance Matrix Service:\n\n- Streaming allows elements to be returned before the entire matrix has been calculated, lowering latency.\n- `ComputeRouteMatrix` has fine-grained options for traffic calculation, letting you make quality-latency tradeoff decisions.\n- Requests are run at a higher priority within Google's infrastructure, which results in higher availability.\n- You can specify heading (direction of travel) and side-of-road information for waypoints.\n- You can request for toll information to be returned, along with route distance and ETA.\n\nEnable the ComputeRouteMatrix method\n------------------------------------\n\nBefore you can use the `ComputeRouteMatrix` method in your code, you must\nenable it. For more information on enabling Routes Preferred API\nmethods, see [Getting Started](/maps/documentation/routes_preferred/get_started).\n| **Important:** Check your quota limits for Routes Preferred API methods. If you need to increase your quota limits, contact support by filing a case on the [Google Maps Platform support page](https://console.cloud.google.com/google/maps-apis/support) in the Google Cloud Console, or by calling the [Google Cloud Support Hotline](/maps/documentation/routes_preferred/support#support_hotline).\n\nUse the ComputeRouteMatrix method\n---------------------------------\n\nThe `ComputeRouteMatrix` method is available through the Routes Preferred\n*v1* endpoint.\n\nSee the following documentation for more detailed information:\n\ngRPC:\n\u003chttps://developers.google.com/maps/documentation/routes_preferred/reference/rpc/google.maps.routes.v1\u003e\n\nREST:\n\u003chttps://developers.google.com/maps/documentation/routes_preferred/reference/rest/v1/TopLevel/computeRouteMatrix\u003e\n\nThe following generic steps describe how to use gRPC to call\nthe `ComputeRouteMatrix` method:\n\n1. Check out the necessary protobuffers from the googleapis repository:\n\n \u003chttps://github.com/googleapis/googleapis/tree/master/google/maps/routes\u003e\n\n Note that some build systems, such as Go, may automatically do this for you.\n2. Using your preferred language and set of build tools, generate the relevant\n protobuffer code.\n\n3. Generate your request. Two required pieces of metadata must be sent\n with the request:\n\n - *X-Goog-Api-Key* must be set to your API key.\n - *X-Goog-Fieldmask* must be set to a comma separated list of the fields\n you require from the response. For example,\n *X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status*.\n You can use the wildcard character (\\*) to denote all fields, but that is\n discouraged. Use the wildcard character to test and understand the\n API response, but explicitly list the fields you want in the field mask\n in your production code.\n\n For more information on how to construct the field mask string, see\n \u003chttps://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto\u003e.\n Note that disabling fields you do not need can reduce latency since\n those fields may require further computation. Your latency will stay\n stable if you specify the fields you need and new fields that require\n more computation are added later.\n4. Send your request to `routespreferred.googleapis.com:443`.You must use\n the TLS protocol.\n\nFor examples of using the `ComputeRouteMatrix` method, see\n[Computing a Route Matrix (Beta) Examples](/maps/documentation/routes_preferred/compute_matrix_examples).\n\nOne distinguishing feature of the `ComputeRouteMatrix` method is that\nerror codes can be returned either for the entire stream or for individual\nelements. For example, the stream connection returns an error if the request\nis malformed (for example, it has zero origins). However, if an error applies\nto just a few elements of the stream (for example, setting an invalid Place ID\nfor an origin), then only the elements affected by the error contain error codes.\n\nThe elements returned by the stream are not guaranteed to be returned\nin any order. Because of this, each element contains an `origin_index`\nand a `destination_index`. For the origins and destinations specified\nby the request, the route origin is equivalent to `origins[origin_index]`\nfor a given element and the route destination is is equivalent\nto `destinations[destination_index]`. These arrays are zero-indexed. It\nis important to store the origin and destination list orders.\n\nCalculating toll fees\n---------------------\n\nFor information on calculating toll fees, see\n[Calculate toll fees](/maps/documentation/routes_preferred/calculate_toll_fees).\n\nFor examples of calculating toll fees, see\n[Computing a Route Matrix Examples](/maps/documentation/routes_preferred/compute_matrix_examples#toll-fee-examples)."]]