تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
المطوّرون في المنطقة الاقتصادية الأوروبية
تعرض Routes API تلقائيًا المسار التلقائي، وهو عادةً
أسرع مسار من نقطة الانطلاق إلى الوجهة. عند طلب مسارات بديلة، تعرض واجهة برمجة التطبيقات ما يصل إلى ثلاثة مسارات بالإضافة إلى المسار التلقائي. يمكن لعملائك بعد ذلك اختيار مسار يناسب متطلباتهم على أفضل وجه.
اعتبارات عند طلب مسارات بديلة
لطلب طرق بديلة، يُرجى مراعاة ما يلي:
يمكنك طلب طرق بديلة فقط للطرق التي لا تتضمّن نقاط توقّف وسيطة. لا يؤدي طلب مسارات بديلة عندما يحدّد المسار نقاطًا وسيطة إلى حدوث خطأ. ومع ذلك، لا يتم عرض أي طرق بديلة.
تحتوي الاستجابة على ثلاثة مسارات بديلة كحدّ أقصى. ومع ذلك، قد لا تتوفّر طرق بديلة في بعض الأحيان، وبالتالي لن يتضمّن الرد سوى المسار التلقائي.
بسبب المعالجة الإضافية المطلوبة لاحتساب الطرق البديلة، قد يؤدي طلب طرق بديلة إلى زيادة وقت استجابة واجهة برمجة التطبيقات.
مثال على طلب مسارات بديلة
اضبط computeAlternativeRoutes على true لطلب مسارات بديلة. يوضّح المثال التالي كيفية طلب مسارات بديلة في طلب طريقة computeRoutes (REST).
في هذا المثال، يمكنك تحديد قناع حقل لكي يحتوي الردّ على السمات duration وdistanceMeters, وrouteLabels فقط لكل مسار. يمكن لعميلك بعد ذلك استخدام هذه المعلومات لاختيار المسار الذي سيسلكه. لمزيد من المعلومات، يُرجى الاطّلاع على اختيار المعلومات التي سيتم عرضها.
مثال على ردّ بشأن المسارات البديلة
في الردّ، استخدِم سمة المصفوفة routeLabels لتحديد مسار الوصول:
بالنسبة إلى المسار التلقائي، تحتوي السمة routeLabels على DEFAULT_ROUTE.
بالنسبة إلى أي طرق بديلة، تحتوي السمة routeLabels على DEFAULT_ROUTE_ALTERNATE.
في هذا المثال، يتضمّن الردّ المسار التلقائي ومسارين بديلين. لاحظ أنّ قيمتَي السمتَين duration وdistanceMeters
تختلفان لكل مسار:
تاريخ التعديل الأخير: 2025-08-10 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-10 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThe Routes API can return up to three alternative routes in addition to the default route, allowing users to choose the best option for their needs.\u003c/p\u003e\n"],["\u003cp\u003eAlternative routes can only be requested for routes without intermediate waypoints and may increase API response time.\u003c/p\u003e\n"],["\u003cp\u003eThe response identifies routes using \u003ccode\u003erouteLabels\u003c/code\u003e: \u003ccode\u003eDEFAULT_ROUTE\u003c/code\u003e for the default route and \u003ccode\u003eDEFAULT_ROUTE_ALTERNATE\u003c/code\u003e for alternative routes.\u003c/p\u003e\n"],["\u003cp\u003eYou can include polylines in the response to display routes on a map by adding \u003ccode\u003eroutes.polyline\u003c/code\u003e to the field mask.\u003c/p\u003e\n"]]],["The Routes API can compute alternative routes by setting `computeAlternativeRoutes` to `true`. It returns up to three alternative routes, besides the default fastest route, for requests without intermediate waypoints. Alternative routes may increase response time and aren't always available. Each route's `routeLabels` array indicates if it's the default (`DEFAULT_ROUTE`) or an alternative (`DEFAULT_ROUTE_ALTERNATE`). You can include route polylines by adding `routes.polyline` in the field mask, enabling visual display on a map.\n"],null,["# Get alternative routes\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| **Note:** Alternative routes are not supported by the Compute Route Matrix feature of the Routes API.\n\nBy default, the Routes API returns the *default* route, which is typically\nthe fastest route from the origin to the destination. When you request\n*alternative* routes, the API returns up to three routes along with the default\nroute. Your customers can then choose a route that best fits their requirements.\n\nConsiderations when requesting alternative routes\n-------------------------------------------------\n\nTo request alternative routes, be aware of the following considerations:\n\n- You can only request alternative routes for routes without intermediate\n waypoints. Requesting alternative routes when the route specifies\n intermediate waypoints does not cause an error. However, no alternative\n routes are returned.\n\n- The response contains a maximum of three alternative routes. However,\n sometimes no alternative routes are available so the response only contains\n the default route.\n\n- Because of the additional processing required to calculate alternative\n routes, requesting alternative routes might increase the response time of\n the API.\n\nExample alternative routes request\n----------------------------------\n\nSet `computeAlternativeRoutes` to `true` to request alternative routes. The\nfollowing example shows how to request alternative routes in a\n[computeRoutes](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRoutes)\nmethod (REST) request. \n\n```json\ncurl -X POST -d '{\n \"origin\":{\n \"location\":{\n \"latLng\":{\n \"latitude\":42.340173523716736,\n \"longitude\":-71.05997968330408\n }\n }\n },\n \"destination\":{\n \"location\":{\n \"latLng\":{\n \"latitude\":42.075698891472804,\n \"longitude\": -72.59806562080408\n }\n }\n },\n \"travelMode\": \"DRIVE\",\n \"routingPreference\":\"TRAFFIC_AWARE\",\n \"computeAlternativeRoutes\": true\n}' \\\n-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.routeLabels' \\\n'https://routes.googleapis.com/directions/v2:computeRoutes'\n```\n\nIn this example, you specify a field mask so that the response contains only the\n`duration`, `distanceMeters,` and `routeLabels` properties for each route. Your\ncustomer can then use that information to choose which route to take. For more\ninformation, see [Choose what information to return](/maps/documentation/routes/choose_fields).\n\nExample alternative routes response\n-----------------------------------\n\nIn the response, use the `routeLabels` array property to identify reach\nroute:\n\n- For the default route, the `routeLabels` property contains\n `DEFAULT_ROUTE`.\n\n- For any alternate routes, the `routeLabels` property contains\n `DEFAULT_ROUTE_ALTERNATE`.\n\n| **Note:** You can also request an eco-friendly route by setting the `requestedReferenceRoutes` property to `FUEL_EFFICIENT` in the request. For the eco-friendly route, the `routeLabels` property contains `FUEL_EFFICIENT`. For more on eco-friendly routing, see [Get eco-friendly routes](/maps/documentation/routes/eco-routes).\n\nIn this example, the response contains the default route and two alternative\nroutes. Notice that the values of the `duration` and `distanceMeters` properties\nare different for each route: \n\n```json\n{\n \"routes\": [\n {\n \"distanceMeters\": 150322,\n \"duration\": \"5309s\",\n \"routeLabels\": [\n \"DEFAULT_ROUTE\"\n ]\n },\n {\n \"distanceMeters\": 157614,\n \"duration\": \"6879s\",\n \"routeLabels\": [\n \"DEFAULT_ROUTE_ALTERNATE\"\n ]\n },\n {\n \"distanceMeters\": 189311,\n \"duration\": \"7376s\",\n \"routeLabels\": [\n \"DEFAULT_ROUTE_ALTERNATE\"\n ]\n }\n ]\n}\n```\n\n### Include polylines in the response\n\nTo make it easier for the user to select the route, add the\n[polyline](/maps/documentation/routes/traffic_on_polylines) for each route to the response. You can then\ndisplay each polyline on a map.\n\nTo add the polyline, include `routes.polyline` in the field mask: \n\n```json\n-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.routeLabels,routes.polyline'\n```\n\nThe response then contains the polyline for each route: \n\n```json\n{\n \"routes\": [\n {\n \"distanceMeters\": 150322,\n \"duration\": \"5309s\",\n \"polyline\": {\n \"encodedPolyline\": \"mrlaGtavpLPLBTm...PgA^qC\"\n },\n \"routeLabels\": [\n \"DEFAULT_ROUTE\"\n ]\n },\n {\n \"distanceMeters\": 157614,\n \"duration\": \"6879s\",\n \"polyline\": {\n \"encodedPolyline\": \"DmEd`@e@pCo@pCs@z...PgA^qC\"\n },\n \"routeLabels\": [\n \"DEFAULT_ROUTE_ALTERNATE\"\n ]\n },\n {\n \"distanceMeters\": 189311,\n \"duration\": \"7376s\",\n \"polyline\": {\n \"encodedPolyline\": \"FVLL|Af@HPAV...PgA^qC\"\n },\n \"routeLabels\": [\n \"DEFAULT_ROUTE_ALTERNATE\"\n ]\n }\n ]\n}\n```"]]