با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
توسعه دهندگان منطقه اقتصادی اروپا (EEA).
از Routes API برای محاسبه مسافت و مدت یک مسیر برای چندین مبدا و مقصد با فراخوانی روش computeRouteMatrix (REST) یا روش جریان ComputeRouteMatrix (gRPC) استفاده کنید.
با توجه به فهرستی از مبدا و مقصد، این روش مسافت و مدت یک مسیر را که از هر مبدأ شروع میشود و به هر مقصد ختم میشود، محاسبه میکند.
روشهای Compute Route Matrix محدودیتهای درخواستی زیر را برای ایستگاههای بین با استفاده از آدرس یا شناسه مکان و برای عناصر اعمال میکنند. عناصر مسیرهای بین هر مبدا و مقصد در یک ماتریس مسیر هستند، بنابراین تعداد عناصر، تعداد مبدا ضربدر تعداد مقصد است. به عنوان مثال، اگر 10 مبدا و 10 مقصد داشته باشید، 100 عنصر دارید:
تعداد عناصر برای مسیرهایی که مسیرهای TRANSIT نیستند نمی تواند از 625 تجاوز کند.
اگر یک مسیر TRANSIT را مشخص کنید ، تعداد عناصر نباید از 100 تجاوز کند.
اگر TRAFFIC_AWARE_OPTIMAL را مشخص کنید ، تعداد عناصر نمی تواند از 100 تجاوز کند. برای اطلاعات بیشتر در مورد TRAFFIC_AWARE_OPTIMAL ، به تعیین نحوه و آیا شامل کردن داده های ترافیک مراجعه کنید.
اگر مبدا یا مقصد را با استفاده از آدرس یا شناسه مکان مشخص کنید ، در مجموع تا 50 را از این طریق مشخص می کنید.
خطاهای پاسخگویی
یکی از ویژگیهای روشهای Compute Route Matrix این است که خطاها را میتوان برای کل پاسخ یا برای عناصر پاسخ جداگانه برگرداند. به عنوان مثال، اگر درخواست بد شکل باشد، کل پاسخ حاوی یک خطا است (مثلاً منشا آن صفر است).
با این حال، اگر یک خطا برای زیرمجموعه ای از عناصر در پاسخ اعمال شود (مثلاً، یک مسیر را نمی توان برای یک ترکیب مبدا و مقصد محاسبه کرد)، آنگاه فقط عناصری که تحت تأثیر خطا قرار گرفته اند، کد خطا را برمی گردانند.
نتایج جریان gRPC
متد ComputeRouteMatrix gRPC فهرستی از مبدا و مقصد را می گیرد و یک جریان حاوی اطلاعات مسیر را برای هر ترکیب مبدا و مقصد برمی گرداند. از آنجایی که نتایج به صورت جریانی برگردانده میشوند، لازم نیست منتظر بمانید تا تمام ترکیبهای مسیر ممکن محاسبه شود تا بتوانید پردازش نتایج را شروع کنید.
عناصر بازگردانده شده توسط جریان تضمینی برای بازگشت به هیچ ترتیبی ندارند. بنابراین، هر عنصر پاسخ شامل یک origin_index و یک destination_index است. برای مبداها و مقصدهای مشخص شده توسط درخواست، مبدأ مسیر معادل origins[origin_index] برای یک عنصر معین و مقصد مسیر معادل destinations[destination_index] است. این آرایه ها دارای شاخص صفر هستند. ذخیره سفارشات لیست مبدا و مقصد مهم است.
مثالهای ماتریس مسیر را محاسبه کنید
از متد computeRouteMatrix در درخواست HTTP برای محاسبه ماتریس مسیر استفاده کنید.
مثال HTTP
مثال زیر یک درخواست HTTP computeRouteMatrix را نشان می دهد. در این مثال شما:
آرایه ای از دو نقطه مبدا و دو نقطه مقصد را مشخص کنید. این روش یک مسیر را از هر مبدأ به هر مقصد محاسبه می کند، بنابراین پاسخ شامل چهار مسیر است.
در آرایه، عنصر اول در شاخص 0، عنصر دوم شاخص 1 و غیره است.
یک ماسک فیلد پاسخ اضافه کنید تا مشخص کنید کدام فیلدهای پاسخ (REST) یا ComputeRoutesResponse (gRPC) باید برگردانده شوند. در این مثال، درخواست را برای بازگرداندن originIndex ، destinationIndex ، duration ، distanceMeters ، status و condition برای هر مسیر پیکربندی کنید. برای اطلاعات بیشتر، به انتخاب فیلدها برای بازگشت مراجعه کنید.
پاسخ شامل چهار مسیر ممکن برای ترکیب همه ایستگاههای مسیر مبدا و مقصد است.
هر مسیر را در پاسخ با استفاده از فیلدهای پاسخ originIndex و destinationIndex شناسایی کنید. به عنوان مثال، یک originIndex 1 در پاسخ مربوط به مسیری است که از نقطه راه در شاخص 1 آرایه origins در درخواست محاسبه می شود.
برای مثال درخواستهای gRPC ، نمونههای مربوط به درخواست gRPC را ببینید. مثال جاوا در آن صفحه هر دو مسیرهای محاسبه و ماتریس مسیر محاسبه را فراخوانی می کند.
تاریخ آخرین بهروزرسانی 2025-06-20 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-06-20 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eGoogle Maps Routes API calculates distance and duration for routes with multiple origins and destinations using the \u003ccode\u003ecomputeRouteMatrix\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eRequest limits apply to the number of elements (routes between origin-destination pairs), varying based on route type and traffic settings.\u003c/p\u003e\n"],["\u003cp\u003eResponses can contain errors for the entire request or individual route elements, providing granular feedback on route calculation success.\u003c/p\u003e\n"],["\u003cp\u003eThe gRPC method offers stream results, allowing processing of route information as it becomes available, without waiting for all calculations to complete.\u003c/p\u003e\n"],["\u003cp\u003eExamples demonstrate how to use the API with HTTP requests and interpret the response, identifying routes using origin and destination indices.\u003c/p\u003e\n"]]],["The Routes API calculates route distances and durations for multiple origins and destinations using `computeRouteMatrix`. It supports REST and gRPC methods, with gRPC offering streaming results. Key actions include specifying origins and destinations, setting travel modes (like transit), and handling request limits (e.g., element counts for `TRANSIT` or `TRAFFIC_AWARE_OPTIMAL` routes). Errors can be response-wide or element-specific. The response provides `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, and `status` for each route.\n"],null,["# Get a route matrix\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nUse the Routes API to calculate the distance and duration of a route for\nmultiple origins and destinations by calling the\n[computeRouteMatrix](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix) method\n(REST) or the streaming\n[ComputeRouteMatrix](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#google.maps.routing.v2.Routes.ComputeRouteMatrix)\nmethod (gRPC).\n\nGiven a list of origins and destinations, the method\ncalculates the distance and duration of a route starting at each origin\nand ending at each destination.\n\n### Get a transit route matrix\n\nYou can also compute a route matrix on transit. For an\nexample, see [Get a route matrix on transit](/maps/documentation/routes/transit-rm).\n\n### Request limits\n\nThe Compute Route Matrix methods enforce the following request\nlimits for waypoints using address or placeIDs, and for elements. Elements are\nthe routes between each origin and destination in a route matrix, so the number\nof elements is the number of origins *times* the number of destinations. For\nexample, if you have 10 origins and 10 destinations, you have 100 elements:\n\n- **The number of elements** cannot exceed 625 for routes\n that are not `TRANSIT` routes.\n\n- **If you specify a `TRANSIT` route**, the number of elements cannot exceed\n 100.\n\n- **If you specify `TRAFFIC_AWARE_OPTIMAL`** , the number of elements cannot\n exceed 100. For more information about `TRAFFIC_AWARE_OPTIMAL`, see\n [Specify how and if to include traffic data](/maps/documentation/routes/config_trade_offs).\n\n- **If you specify origins or destinations using address or place ID**,\n you specify up to 50 total this way.\n\n### Response errors\n\nOne feature of the Compute Route Matrix methods is that errors can\nbe returned either for the entire response or for individual response elements.\nFor example, the entire response contains an error if the request is malformed\n(for example, it has zero origins).\n\nHowever, if an error applies to a subset of\nelements in the response (for example, a route cannot be computed for one\ncombination of origin and destination), then only the elements affected by the\nerror return an error code.\n\n### gRPC Stream results\n\nThe [ComputeRouteMatrix](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#google.maps.routing.v2.Routes.ComputeRouteMatrix)\ngRPC method takes in a list of origins and destinations and returns a stream\ncontaining route information for each combination of origin and destination.\nBecause the results are returned as a stream, you don't have to wait until all\npossible route combinations are calculated before you can start to process the\nresults.\n| **Note:** Stream results are not supported by the REST API.\n\nThe elements returned by the stream are not guaranteed to be returned in any\norder. Therefore, each response element contains an `origin_index` and a\n`destination_index`. For the origins and destinations specified by the request,\nthe route origin is equivalent to `origins[origin_index]` for a given element\nand the route destination is equivalent to `destinations[destination_index]`.\nThese arrays are zero-indexed. It is important to store the origin and\ndestination list orders.\n\nCompute a Route Matrix Examples\n-------------------------------\n\nUse the [computeRouteMatrix](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix)\nmethod in an HTTP request to compute a route matrix.\n\n### HTTP example\n\nThe following example shows a `computeRouteMatrix` HTTP request. In this example\nyou:\n\n- Specify an array of two origin and two destination waypoints. The method\n calculates a route from each origin to each destination so the response\n contains four routes.\n\n In the array, the first element is at an index of 0, the second is index 1,\n and so forth.\n- Include a response field mask to specify which fields of the\n [response](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix#response-body)\n (REST) or\n [ComputeRoutesResponse](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#computeroutesresponse)\n (gRPC) to return. In this example, configure the request to return\n `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, `status`,\n and `condition` for each route. For more information, see\n [Choose fields to return](/maps/documentation/routes/choose_fields).\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 \"routeModifiers\": { \"avoid_ferries\": true}\n },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.403184,\n \"longitude\": -122.097371\n }\n }\n },\n \"routeModifiers\": { \"avoid_ferries\": true}\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\": \"DRIVE\",\n \"routingPreference\": \"TRAFFIC_AWARE\"\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' \\\n'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'\n```\n\nThe response contains the four possible routes for the combination of all origin\nand destination waypoints.\n| **Note:** For detailed description of the response, see [Understand the compute\n| route matrix response](/maps/documentation/routes/understand-rm-response).\n\nIdentify each route in the response by using the `originIndex` and\n`destinationIndex` response fields. For example, an `originIndex` of 1 in the\nresponse corresponds to a route calculated from the waypoint at index 1 of the\n`origins` array in the request. \n\n```json\n[\n {\n \"originIndex\": 0,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 822,\n \"duration\": \"160s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 1,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 2919,\n \"duration\": \"361s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 1,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 5598,\n \"duration\": \"402s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 0,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 7259,\n \"duration\": \"712s\",\n \"condition\": \"ROUTE_EXISTS\"\n }\n]\n```\n\n### gRPC examples\n\nFor example [gRPC](https://www.grpc.io/) requests, see the examples on\n[Example gRPC request](/maps/documentation/routes/compute_route_directions#grpc-distance-example). The Java\nexample on that page calls both the Compute Routes and\nCompute Route Matrix."]]