Routes Preferred API 目前仅适用于部分客户。如需了解详情,请
与销售人员联系。
计算路线矩阵
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Routes Preferred API 的 ComputeRouteMatrix
方法计算多个出发地和目的地的路线距离和时长。ComputeRouteMatrix
同时支持流式 gRPC 调用和 REST HTTP 调用。
给定一个出发地和目的地对的列表,ComputeRouteMatrix
会计算从每个出发地出发并到达每个目的地的路线的距离和时长。流的每个元素都对应于一条路线的信息。
与距离矩阵服务相比,ComputeRouteMatrix
方法具有以下几项优势:
- 通过流式传输,可以在计算出整个矩阵之前返回元素,从而缩短延迟时间。
ComputeRouteMatrix
具有精细的流量计算选项,可让您在质量与延迟之间做出权衡。
- 请求在 Google 的基础架构中以更高的优先级运行,从而实现更高的可用性。
- 您可以为途经点指定航向(行驶方向)和道路一侧信息。
- 您可以请求返回路程距离和预计到达时间 (ETA),以及过路费信息。
启用 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
请注意,某些 build 系统(例如 Go)可能会自动为您执行此操作。
使用您偏好的语言和一组构建工具,生成相关的 Protobuf 代码。
生成您的请求。必须随请求发送两项必需的元数据:
将请求发送到 routespreferred.googleapis.com:443
。您必须使用 TLS 协议。
如需查看使用 ComputeRouteMatrix
方法的示例,请参阅计算路线矩阵(Beta 版)示例。
ComputeRouteMatrix
方法的一个显著特点是,可以针对整个流或单个元素返回错误代码。例如,如果请求格式有误(例如,来源为零),则流连接会返回错误。不过,如果错误仅适用于信息流中的少数几个元素(例如,为来源设置了无效的地点 ID),则只有受错误影响的元素包含错误代码。
无法保证数据流返回的元素的顺序。因此,每个元素都包含 origin_index
和 destination_index
。对于请求指定的出发地和目的地,给定元素的路线出发地相当于 origins[origin_index]
,路线目的地相当于 destinations[destination_index]
。这些数组从零开始编制索引。请务必存储来源和目的地列表顺序。
计算通行费
如需了解如何计算过路费,请参阅计算过路费。
如需查看计算过路费的示例,请参阅计算路线矩阵示例。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-10。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-10。"],[[["\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,[]]