Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể tính toán khoảng cách và thời lượng của một tuyến đường cho nhiều điểm xuất phát và điểm đến bằng cách sử dụng phương thức ComputeRouteMatrix của Routes Preferred API. ComputeRouteMatrix hỗ trợ cả lệnh gọi gRPC truyền phát trực tiếp và lệnh gọi HTTP REST.
Với danh sách các cặp điểm xuất phát và điểm đến, ComputeRouteMatrix sẽ tính toán khoảng cách và thời lượng của một tuyến đường bắt đầu tại mỗi điểm xuất phát và kết thúc tại mỗi điểm đến. Mỗi phần tử của luồng tương ứng với thông tin cho một tuyến đường duy nhất.
Phương thức ComputeRouteMatrix có một số ưu điểm so với Distance Matrix Service:
Truyền phát trực tuyến cho phép trả về các phần tử trước khi toàn bộ ma trận được tính toán, giúp giảm độ trễ.
ComputeRouteMatrix có các lựa chọn chi tiết để tính toán lưu lượng truy cập, cho phép bạn đưa ra quyết định đánh đổi giữa chất lượng và độ trễ.
Các yêu cầu được chạy ở mức độ ưu tiên cao hơn trong cơ sở hạ tầng của Google, dẫn đến khả năng cung cấp cao hơn.
Bạn có thể chỉ định thông tin về hướng (hướng di chuyển) và vị trí bên đường cho các điểm tham chiếu.
Bạn có thể yêu cầu trả về thông tin về phí cầu đường, cùng với khoảng cách và thời gian đến dự kiến của tuyến đường.
Bật phương thức ComputeRouteMatrix
Trước khi có thể sử dụng phương thức ComputeRouteMatrix trong mã, bạn phải bật phương thức này. Để biết thêm thông tin về cách bật các phương thức Routes Preferred API, hãy xem phần Bắt đầu sử dụng.
Sử dụng phương thức ComputeRouteMatrix
Phương thức ComputeRouteMatrix có sẵn thông qua điểm cuối Routes Preferred v1.
Hãy xem tài liệu sau để biết thêm thông tin chi tiết:
Xin lưu ý rằng một số hệ thống xây dựng (chẳng hạn như Go) có thể tự động thực hiện việc này cho bạn.
Sử dụng ngôn ngữ và bộ công cụ xây dựng mà bạn muốn, hãy tạo mã protobuffer có liên quan.
Tạo yêu cầu của bạn. Bạn phải gửi 2 phần siêu dữ liệu bắt buộc cùng với yêu cầu:
Bạn phải đặt X-Goog-Api-Key thành khoá API của mình.
Bạn phải đặt X-Goog-Fieldmask thành danh sách các trường được phân tách bằng dấu phẩy mà bạn yêu cầu trong phản hồi. Ví dụ: X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status.
Bạn có thể sử dụng ký tự đại diện (*) để biểu thị tất cả các trường, nhưng không nên làm như vậy. Sử dụng ký tự đại diện để kiểm thử và tìm hiểu phản hồi API, nhưng hãy liệt kê rõ ràng các trường bạn muốn trong mặt nạ trường trong mã sản xuất của bạn.
Để biết thêm thông tin về cách tạo chuỗi mặt nạ trường, hãy xem https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto.
Xin lưu ý rằng việc tắt các trường không cần thiết có thể giảm độ trễ vì những trường đó có thể yêu cầu tính toán thêm. Độ trễ của bạn sẽ ổn định nếu bạn chỉ định các trường cần thiết và các trường mới cần thêm nhiều phép tính hơn sẽ được thêm sau.
Gửi yêu cầu của bạn đến routespreferred.googleapis.com:443.Bạn phải sử dụng giao thức TLS.
Một điểm khác biệt của phương thức ComputeRouteMatrix là mã lỗi có thể được trả về cho toàn bộ luồng hoặc cho từng phần tử riêng lẻ. Ví dụ: kết nối truyền trực tuyến sẽ trả về lỗi nếu yêu cầu có định dạng không đúng (ví dụ: có 0 nguồn). Tuy nhiên, nếu lỗi chỉ áp dụng cho một số phần tử của luồng (ví dụ: đặt Mã địa điểm không hợp lệ cho một nguồn), thì chỉ những phần tử bị ảnh hưởng bởi lỗi mới chứa mã lỗi.
Các phần tử do luồng trả về không được đảm bảo sẽ được trả về theo bất kỳ thứ tự nào. Do đó, mỗi phần tử đều chứa một origin_index và một destination_index. Đối với điểm khởi hành và điểm đến do yêu cầu chỉ định, điểm khởi hành của tuyến đường tương đương với origins[origin_index] cho một phần tử nhất định và điểm đến của tuyến đường tương đương với destinations[destination_index]. Các mảng này được lập chỉ mục từ 0. Điều quan trọng là phải lưu trữ thứ tự danh sách nguồn và đích đến.
Tính phí cầu đường
Để biết thông tin về cách tính phí cầu đường, hãy xem phần Tính phí cầu đường.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-10 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)."]]