获取公交矩阵
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
获取使用相应区域内可用公共交通选项的公交路线矩阵。公交选项可能包括公交车、地铁和火车等。
如需了解详情,请参阅获取公交路线。
公交路线矩阵示例
以下示例计算了公交路线矩阵,除了路线矩阵的典型字段掩码之外,还请求了公交费用:
curl -X POST -d '{
"origins": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420761,
"longitude": -122.081356
}
}
}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.403184,
"longitude": -122.097371
}
}
}
}
],
"destinations": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420999,
"longitude": -122.086894
}
}
}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.383047,
"longitude": -122.044651
}
}
}
}
],
"travelMode": "TRANSIT"
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status,condition,travelAdvisory.transitFare' \
'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'
公交路线矩阵响应
以下是公交路线矩阵请求的响应:
[
{
"originIndex": 1,
"destinationIndex": 1,
"status": {},
"distanceMeters": 8709,
"duration": "2754s",
"travelAdvisory": {
"transitFare": {
"currencyCode": "USD",
"units": "2",
"nanos": 500000000
}
},
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 0,
"destinationIndex": 0,
"status": {},
"distanceMeters": 530,
"duration": "394s",
"travelAdvisory": {
"transitFare": {}
},
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 1,
"destinationIndex": 0,
"status": {},
"distanceMeters": 2934,
"duration": "740s",
"travelAdvisory": {
"transitFare": {
"currencyCode": "USD",
"units": "2",
"nanos": 500000000
}
},
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 0,
"destinationIndex": 1,
"status": {},
"distanceMeters": 9883,
"duration": "3049s",
"travelAdvisory": {
"transitFare": {
"currencyCode": "USD",
"units": "5"
}
},
"condition": "ROUTE_EXISTS"
}
]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-17。
[null,null,["最后更新时间 (UTC):2025-08-17。"],[[["\u003cp\u003eObtain a matrix of routes using available public transportation like buses, subways, and trains for a specified region.\u003c/p\u003e\n"],["\u003cp\u003eThe API allows you to calculate travel distances, durations, and even transit fares between multiple origins and destinations using public transit.\u003c/p\u003e\n"],["\u003cp\u003eExample code demonstrates how to make a request to the API using curl, specifying origins, destinations, and desired fields like transit fare.\u003c/p\u003e\n"],["\u003cp\u003eThe response provides a matrix with details for each origin-destination pair, including distance, duration, fare (if applicable), and route existence status.\u003c/p\u003e\n"]]],["This content demonstrates how to obtain a transit route matrix using public transportation like buses, subways, and trains. The process involves sending a POST request to the `computeRouteMatrix` endpoint with origin and destination waypoints, specifying `TRANSIT` as the `travelMode`. The request can include a transit fare request. The response provides details such as distance, duration, status, and transit fare information between specified origin and destination pairs.\n"],null,["# Get a route matrix on transit\n\nGet a transit route matrix that uses the public transportation options available\nin the region. Transit options may include buses, subways, and trains, among\nothers.\n\nFor additional details, see [Get a route on transit](/maps/documentation/routes/transit-route).\n\nExample transit route matrix\n----------------------------\n\nThe following example computes a route matrix on transit, requesting the transit\nfare in addition to field masks typical for a route matrix: \n\n```json\ncurl -X POST -d '{\n \"origins\": [\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.420761,\n \"longitude\": -122.081356\n }\n }\n }\n },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.403184,\n \"longitude\": -122.097371\n }\n }\n }\n }\n ],\n \"destinations\": [\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.420999,\n \"longitude\": -122.086894\n }\n }\n }\n },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.383047,\n \"longitude\": -122.044651\n }\n }\n }\n }\n ],\n \"travelMode\": \"TRANSIT\"\n}' \\\n-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n-H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status,condition,travelAdvisory.transitFare' \\\n'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'\n```\n\n### Transit route matrix response\n\nHere is the response from the transit route matrix request: \n\n```json\n[\n {\n \"originIndex\": 1,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 8709,\n \"duration\": \"2754s\",\n \"travelAdvisory\": {\n \"transitFare\": {\n \"currencyCode\": \"USD\",\n \"units\": \"2\",\n \"nanos\": 500000000\n }\n },\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 0,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 530,\n \"duration\": \"394s\",\n \"travelAdvisory\": {\n \"transitFare\": {}\n },\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 1,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 2934,\n \"duration\": \"740s\",\n \"travelAdvisory\": {\n \"transitFare\": {\n \"currencyCode\": \"USD\",\n \"units\": \"2\",\n \"nanos\": 500000000\n }\n },\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 0,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 9883,\n \"duration\": \"3049s\",\n \"travelAdvisory\": {\n \"transitFare\": {\n \"currencyCode\": \"USD\",\n \"units\": \"5\"\n }\n },\n \"condition\": \"ROUTE_EXISTS\"\n }\n]\n```"]]