Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Sviluppatori dello Spazio economico europeo (SEE)
Per impostazione predefinita, l'API Routes restituisce l'itinerario predefinito, che in genere è
il più veloce dal punto di partenza alla destinazione. Quando richiedi
percorsi alternativi, l'API restituisce fino a tre percorsi insieme a quello
predefinito. I tuoi clienti possono quindi scegliere un itinerario che soddisfi al meglio i loro requisiti.
Considerazioni quando si richiedono percorsi alternativi
Per richiedere percorsi alternativi, tieni presente le seguenti considerazioni:
Puoi richiedere percorsi alternativi solo per i percorsi senza waypoint intermedi. La richiesta di percorsi alternativi quando il percorso specifica
tappe intermedie non causa un errore. Tuttavia, non vengono restituiti
percorsi alternativi.
La risposta contiene un massimo di tre itinerari alternativi. Tuttavia,
a volte non sono disponibili percorsi alternativi, quindi la risposta contiene solo
il percorso predefinito.
A causa dell'elaborazione aggiuntiva necessaria per calcolare i percorsi alternativi, la richiesta di percorsi alternativi potrebbe aumentare il tempo di risposta dell'API.
Esempio di richiesta di percorsi alternativi
Imposta computeAlternativeRoutes su true per richiedere itinerari alternativi. L'esempio seguente mostra come richiedere percorsi alternativi in una richiesta del metodo computeRoutes (REST).
In questo esempio, specifichi una maschera di campo in modo che la risposta contenga solo le proprietà
duration, distanceMeters, e routeLabels per ogni percorso. Il tuo
cliente può quindi utilizzare queste informazioni per scegliere quale percorso intraprendere. Per ulteriori
informazioni, vedi Scegliere le informazioni da restituire.
Esempio di risposta di percorsi alternativi
Nella risposta, utilizza la proprietà dell'array routeLabels per identificare la route di raggiungimento:
Per la route predefinita, la proprietà routeLabels contiene
DEFAULT_ROUTE.
Per tutti i percorsi alternativi, la proprietà routeLabels contiene
DEFAULT_ROUTE_ALTERNATE.
In questo esempio, la risposta contiene il percorso predefinito e due percorsi alternativi. Nota che i valori delle proprietà duration e distanceMeters
sono diversi per ogni percorso:
Per facilitare la selezione del percorso da parte dell'utente, aggiungi la
polilinea per ogni percorso alla risposta. Puoi quindi
visualizzare ogni polilinea su una mappa.
Per aggiungere la polilinea, includi routes.polyline nella maschera del campo:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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```"]]