Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Developer Wilayah Ekonomi Eropa (EEA)
Secara default, Routes API menampilkan rute default, yang biasanya merupakan rute tercepat dari titik asal ke tujuan. Saat Anda meminta rute
alternatif, API akan menampilkan hingga tiga rute beserta rute
default. Kemudian, pelanggan Anda dapat memilih rute yang paling sesuai dengan kebutuhan mereka.
Pertimbangan saat meminta rute alternatif
Untuk meminta rute alternatif, perhatikan pertimbangan berikut:
Anda hanya dapat meminta rute alternatif untuk rute tanpa titik jalan perantara. Meminta rute alternatif saat rute menentukan titik jalan perantara tidak menyebabkan error. Namun, tidak ada rute alternatif yang ditampilkan.
Respons berisi maksimal tiga rute alternatif. Namun,
terkadang tidak ada rute alternatif yang tersedia sehingga respons hanya berisi
rute default.
Karena pemrosesan tambahan yang diperlukan untuk menghitung rute alternatif, meminta rute alternatif dapat meningkatkan waktu respons API.
Contoh permintaan rute alternatif
Tetapkan computeAlternativeRoutes ke true untuk meminta rute alternatif. Contoh
berikut menunjukkan cara meminta rute alternatif dalam permintaan
metode computeRoutes (REST).
Dalam contoh ini, Anda menentukan mask kolom sehingga respons hanya berisi properti
duration, distanceMeters,, dan routeLabels untuk setiap rute. Pelanggan Anda kemudian dapat menggunakan informasi tersebut untuk memilih rute yang akan diambil. Untuk informasi
selengkapnya, lihat Memilih informasi yang akan ditampilkan.
Contoh respons rute alternatif
Dalam respons, gunakan properti array routeLabels untuk mengidentifikasi rute
jangkauan:
Untuk rute default, properti routeLabels berisi
DEFAULT_ROUTE.
Untuk rute alternatif, properti routeLabels berisi
DEFAULT_ROUTE_ALTERNATE.
Dalam contoh ini, respons berisi rute default dan dua rute alternatif. Perhatikan bahwa nilai properti duration dan distanceMeters
berbeda untuk setiap rute:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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```"]]