קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
מפתחים באזור הכלכלי האירופי (EEA)
כברירת מחדל, Routes API מחזיר את המסלול המוגדר כברירת מחדל, שהוא בדרך כלל המסלול הכי מהיר מהמוצא ליעד. כשמבקשים מסלולים חלופיים, ה-API מחזיר עד שלושה מסלולים בנוסף למסלול ברירת המחדל. הלקוחות שלכם יכולים לבחור את המסלול שהכי מתאים לדרישות שלהם.
שיקולים כשמבקשים מסלולים חלופיים
כדי לבקש מסלולים חלופיים, חשוב לשים לב לשיקולים הבאים:
אפשר לבקש מסלולים חלופיים רק למסלולים בלי נקודות ציון ביניים. בקשה למסלולים חלופיים כשהמסלול מציין נקודות ציון ביניים לא גורמת לשגיאה. עם זאת, לא מוחזרות מסלולים חלופיים.
התשובה מכילה עד שלוש חלופות מסלול. עם זאת, לפעמים אין מסלולים חלופיים זמינים, ולכן התשובה מכילה רק את מסלול ברירת המחדל.
בגלל העיבוד הנוסף שנדרש כדי לחשב מסלולים חלופיים, בקשה למסלולים חלופיים עלולה להאריך את זמן התגובה של ה-API.
דוגמה לבקשה של מסלולים חלופיים
מגדירים את computeAlternativeRoutes לערך true כדי לבקש מסלולים חלופיים. בדוגמה הבאה מוצגות בקשות לחישוב מסלולים חלופיים בבקשת method computeRoutes (REST).
בדוגמה הזו, מציינים מסכת שדות כדי שהתשובה תכיל רק את המאפיינים duration, distanceMeters, ו-routeLabels של כל מסלול. הלקוח יוכל להשתמש במידע הזה כדי לבחור את המסלול שבו הוא רוצה לנסוע. מידע נוסף זמין במאמר בחירת המידע שיוחזר.
דוגמה לתגובה לגבי מסלולים חלופיים
בתשובה, משתמשים במאפיין המערך routeLabels כדי לזהות את נתיב ההגעה:
בנתיב ברירת המחדל, המאפיין routeLabels מכיל את הערך DEFAULT_ROUTE.
בכל המסלולים החלופיים, המאפיין routeLabels מכיל את הערך DEFAULT_ROUTE_ALTERNATE.
בדוגמה הזו, התשובה מכילה את מסלול ברירת המחדל ושני מסלולים חלופיים. שימו לב שהערכים של המאפיינים duration ו-distanceMeters שונים בכל מסלול:
[[["התוכן קל להבנה","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"]],["עדכון אחרון: 2025-09-04 (שעון UTC)."],[[["\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```"]]