Chỉ định loại mô hình lưu lượng truy cập để sử dụng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nhà phát triển ở Khu vực kinh tế Châu Âu (EEA)
Các mô hình lưu lượng truy cập sử dụng các thuật toán và yếu tố cụ thể để dự đoán mức độ ảnh hưởng của tình trạng giao thông đến tổng thời gian di chuyển cho một tuyến đường nhất định. Routes API cung cấp nhiều loại mô hình lưu lượng truy cập để ưu tiên các yếu tố khác nhau được dùng để tính toán thời lượng trong lưu lượng truy cập. Bạn có thể chỉ định loại mô hình giao thông mà bạn muốn tuyến đường sử dụng khi tính toán thời lượng cho tuyến đường hoặc ma trận tuyến đường. Khi bạn đặt loại mô hình lưu lượng truy cập, giá trị được trả về trong trường duration sẽ thay đổi tuỳ thuộc vào mô hình lưu lượng truy cập mà bạn chọn.
Cả phương thức Routes API Compute Routes và Compute Route Matrix đều hỗ trợ các loại mô hình lưu lượng truy cập.
Theo mặc định, cả hai phương thức đều sử dụng loại mô hình lưu lượng truy cập BEST_GUESS.
Cách chỉ định loại lưu lượng truy cập
Đặt điểm xuất phát và điểm đến.
Nếu thời gian khởi hành của bạn là một ngày trong tương lai, hãy thêm thời gian bằng cách sử dụng tham số departure_time. Nếu bạn không chỉ định giờ khởi hành, thì giờ mặc định sẽ là thời điểm hiện tại.
Đặt tham số routingPreference thành TRAFFIC_AWARE_OPTIMAL. Việc đặt routingPreference thành TRAFFIC_UNAWARE hoặc TRAFFIC_AWARE không tương thích với tham số trafficModel.
Chọn mô hình lưu lượng truy cập để sử dụng với tham số trafficModel và một trong các loại sau:
BEST_GUESS (mặc định) để yêu cầu duration_in_traffic được trả về là thông tin ước tính chính xác nhất về thời gian di chuyển dựa trên những thông tin đã biết về cả tình trạng giao thông trong quá khứ và tình trạng giao thông theo thời gian thực. Thông tin giao thông trực tiếp càng trở nên quan trọng hơn khi departure_time càng gần với thời điểm hiện tại. Việc sử dụng loại BEST_GUESS mặc định sẽ đưa ra những dự đoán hữu ích nhất cho phần lớn các trường hợp sử dụng.
PESSIMISTIC để yêu cầu duration_in_traffic được trả về mô phỏng thời gian dựa trên tình trạng lưu lượng truy cập trong quá khứ vào những ngày có lưu lượng truy cập không hợp lệ.
Loại này cho biết thời lượng ước tính có khả năng dài hơn thời gian di chuyển thực tế vào hầu hết các ngày. Vào những ngày có điều kiện thời tiết đặc biệt xấu, số lượng yêu cầu có thể vẫn vượt quá mức ước tính này.
OPTIMISTIC để yêu cầu duration_in_traffic được trả về mô phỏng thời gian dựa trên tình trạng lưu lượng truy cập trong quá khứ vào những ngày có lưu lượng truy cập tốt.
Loại này đưa ra thời lượng ước tính có khả năng ngắn hơn thời gian di chuyển thực tế vào hầu hết các ngày. Vào những ngày có điều kiện đặc biệt thuận lợi, thời gian sạc có thể nhanh hơn so với ước tính này.
Ví dụ:
"trafficModel":"OPTIMISTIC"
Chỉ định trường routes.duration trong mặt nạ trường.
REST
-HX-Goog-FieldMask:routes.duration
RPC
const(fieldMask="routes.duration")
Khi ước tính thời gian di chuyển trong điều kiện giao thông, Routes API sẽ sử dụng loại mô hình giao thông mà bạn đã yêu cầu.
Ví dụ: Yêu cầu về mô hình lưu lượng truy cập
Ví dụ: yêu cầu này chỉ định rằng thời lượng được ước tính bằng cách sử dụng loại mô hình lưu lượng truy cập OPTIMISTIC tại một thời gian khởi hành cụ thể:
[null,null,["Cập nhật lần gần đây nhất: 2025-07-30 UTC."],[[["\u003cp\u003eThe Routes API offers different traffic models (\u003ccode\u003eBEST_GUESS\u003c/code\u003e, \u003ccode\u003ePESSIMISTIC\u003c/code\u003e, \u003ccode\u003eOPTIMISTIC\u003c/code\u003e) to predict travel times based on various traffic conditions.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify the desired traffic model when calculating routes, with \u003ccode\u003eBEST_GUESS\u003c/code\u003e being the default, providing the most useful predictions for most use cases.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ePESSIMISTIC\u003c/code\u003e models travel time based on bad traffic days, likely resulting in a longer estimated duration, while \u003ccode\u003eOPTIMISTIC\u003c/code\u003e uses good traffic days, potentially resulting in a shorter duration.\u003c/p\u003e\n"],["\u003cp\u003eLive traffic information is integrated into the \u003ccode\u003eBEST_GUESS\u003c/code\u003e model, making it potentially shorter than \u003ccode\u003eOPTIMISTIC\u003c/code\u003e or longer than \u003ccode\u003ePESSIMISTIC\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo use traffic models, include the \u003ccode\u003etrafficModel\u003c/code\u003e parameter in your request and specify \u003ccode\u003eroutes.duration\u003c/code\u003e in the field mask.\u003c/p\u003e\n"]]],["The Routes API's traffic models predict travel time, offering `BEST_GUESS` (default), `PESSIMISTIC` (longer), and `OPTIMISTIC` (shorter) durations. To use, set the origin, destination, and optional `departure_time`. Choose a `trafficModel` type and specify `routes.duration` in the field mask. The API calculates traffic-affected durations based on the selected model. Using `BEST_GUESS` integrate live information. The default is `BEST_GUESS`, and the duration returned varies depending on the chosen model. The service is pre-GA, it might have limited support and changes.\n"],null,["# Specify the traffic model type to use\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\n\u003cbr /\u003e\n\n| This product or feature is Experimental (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage descriptions](/maps/launch-stages).\n\n\u003cbr /\u003e\n\nTraffic models use specific algorithms and factors to predict how traffic\nconditions impact total travel time for a given route. The Routes API\noffers different traffic model types to prioritize different factors used in\ncalculating duration in traffic. You can specify the traffic model type you'd\nlike your routes to use when calculating the duration for your routes or route\nmatrix. When you set the traffic model type, the value returned in the\n`duration` field varies depending on the traffic model you choose.\n\nBoth the Routes API `Compute Routes` and\n`Compute Route Matrix` methods support traffic model types.\n\nBy default, both methods use the `BEST_GUESS` traffic model type.\n\nTo specify a traffic type\n-------------------------\n\n1. Set your origin and destination.\n\n2. If your departure time is in the future, include the time using the\n `departure_time` parameter. If you don't specify a departure time, it\n defaults to now.\n\n3. Set the `routingPreference` parameter to `TRAFFIC_AWARE_OPTIMAL`. Setting the\n `routingPreference` to `TRAFFIC_UNAWARE` or `TRAFFIC_AWARE` is incompatible\n with the `trafficModel` parameter.\n\n4. Select the traffic model to use with the `trafficModel` parameter and\n one of the following types:\n\n - `BEST_GUESS` (default) to request that the returned `duration_in_traffic`\n is the best estimate of travel time given what is known about both\n historical traffic conditions and live traffic. Live traffic becomes more\n important the closer the `departure_time` is to now. Using the default type\n of `BEST_GUESS` gives the most useful predictions for the vast majority of\n use cases.\n\n - `PESSIMISTIC` to request that the returned `duration_in_traffic` models the\n time based on the historical traffic conditions on bad traffic days.\n This type gives an estimated duration that is most likely **longer** than\n the actual travel time on most days. Occasional days with particularly bad\n conditions may still exceed this estimate.\n\n - `OPTIMISTIC` to request that the returned `duration_in_traffic` models the\n time based on the historical traffic conditions on good traffic days.\n This type gives an estimated duration that is most likely **shorter** than\n the actual travel time on most days. Occasional days with particularly\n good conditions may still be faster than this estimate.\n\n For example: \n\n ```json\n \"trafficModel\": \"OPTIMISTIC\"\n ```\n\n \u003cbr /\u003e\n\n | **Note:** It is possible the `BEST_GUESS` travel time prediction may be shorter than `OPTIMISTIC` or longer than `PESSIMISTIC`, due to the way `BEST_GUESS` integrates live traffic information.\n5. Specify the `routes.duration` field in the field mask.\n\n **REST** \n\n ```json\n -H X-Goog-FieldMask: routes.duration\n ```\n\n **RPC** \n\n ```gdscript\n const (fieldMask = \"routes.duration\")\n ```\n\nWhen the Routes API estimates time in traffic, it uses the traffic\nmodel type you've requested.\n\nExample: Traffic model request\n------------------------------\n\nFor example, this request specifies that the duration be estimated using the\n`OPTIMISTIC` traffic model type at a specific departure time: \n\n```json\ncurl -X POST -H 'content-type: application/json' -d ' {\n\"origin\": {\n \"address\": \"Kyoto, Japan\"\n },\n\"destination\": {\n \"placeId\": \"ChIJrYtcv-urAWAR3XzWvXv8n_s\"\n },\n\"travelMode\": \"DRIVE\",\n\"routingPreference\": \"TRAFFIC_AWARE_OPTIMAL\",\n\"trafficModel\": \"OPTIMISTIC\"\n}' \\\n-H 'Content-Type: application/json' \\\n-H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n-H 'X-Goog-FieldMask: routes.duration' \\\n'https://routes.googleapis.com/directions/v2:computeRoutes'\n```\n\nThis request returns the estimated duration using the `OPTIMISTIC` traffic model\ntype for the trip: \n\n```json\n\"duration\": \"1238s\"\n```\n\nIf you change the traffic model type to `PESSIMISTIC`, the returned duration is\nlonger: \n\n```json\n\"duration\": \"2436s\"\n```\n| **Note:** If you do not specify a `departureTime`, the implied departure is the request time. This means that you may get different values depending on when the request is sent."]]