Tổng quan về tính toán tuyến đườ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)

Giới thiệu
Tính toán tuyến đường là một phương thức trong dịch vụ Routes API (API tuyến đường) chấp nhận yêu cầu HTTPS và trả về tuyến đường lý tưởng giữa hai vị trí.
Cung cấp đường đi có tình hình giao thông theo thời gian thực cho phương tiện công cộng, xe đạp, ô tô, xe máy 2 bánh hoặc đi bộ giữa nhiều vị trí.
Bạn cần Ma trận tuyến đường? Nếu bạn quan tâm đến ma trận tuyến đường, hãy xem bài viết Tổng quan về ma trận tuyến đường.
Đang di chuyển? Nếu bạn đang di chuyển từ API Distance Matrix (Cũ), hãy xem hướng dẫn di chuyển bắt đầu bằng phần Lý do di chuyển sang Routes API.
Lý do sử dụng Tuyến điện toán
Với tính năng Tính toán tuyến đường, bạn có thể định tuyến xe hoặc gói theo các thông tin chi tiết về tuyến đường mà bạn muốn, đồng thời tối ưu hoá chi phí và chất lượng.
Bạn có thể làm gì với Tuyến tính toán?
Với phương thức Compute Routes
của API tuyến đường, bạn có thể làm những việc sau:
Xem đường đi cho nhiều phương tiện di chuyển và cho một hoặc nhiều đích đến:
Phương tiện đi lại: phương tiện công cộng, lái xe, xe hai bánh, đi bộ hoặc đi xe đạp.
Một loạt các điểm trung gian mà bạn có thể tối ưu hoá để có thứ tự di chuyển hiệu quả nhất đến các điểm đó.
Sử dụng nhiều cách để chỉ định điểm xuất phát, điểm đến và điểm trung gian:
Chuỗi văn bản. Ví dụ: "Chicago, IL", "Darwin, NT, Australia", "1800
Amphitheatre Parkway, Mountain View, CA 94043" hoặc "CWF6+FWX Mountain
View, California"
Mã địa điểm
Toạ độ vĩ độ và kinh độ, tuỳ chọn có hướng xe
Chỉnh sửa các lựa chọn cho tuyến đường dựa trên nhu cầu và mục tiêu của bạn:
Chọn tuyến đường tiết kiệm nhiên liệu hoặc tiết kiệm năng lượng cho loại động cơ của xe: Dầu diesel, Điện, Kết hợp, Xăng. Để biết thêm thông tin, hãy xem bài viết Lấy tuyến đường thân thiện với môi trường.
Đặt các tuỳ chọn chi tiết để tính toán lưu lượng truy cập, cho phép bạn đưa ra quyết định đánh đổi chất lượng so với độ trễ. Để biết thông tin chi tiết, hãy xem phần Chỉ định cách thức và liệu có nên đưa dữ liệu lưu lượng truy cập vào hay không.
Thiết lập hướng xe (hướng di chuyển) và thông tin bên đường cho các điểm trung gian để tăng độ chính xác của ETA. Để biết thông tin chi tiết, hãy xem phần Chỉ định hướng và bên đường của xe.
Chỉ định vị trí đi qua so với vị trí đầu cuối và vị trí dừng an toàn. Để biết thông tin chi tiết, hãy xem bài viết Đặt điểm dừng dọc theo tuyến đường và Đặt điểm để tuyến đường đi qua.
Yêu cầu thông tin về phí cầu đường, cùng với quãng đường và giờ đến dự kiến. Để biết thông tin chi tiết, hãy xem phần Tính phí cầu đường cho một tuyến đường.
Kiểm soát độ trễ và chất lượng bằng cách chỉ yêu cầu dữ liệu bạn cần bằng cách sử dụng mặt nạ trường. Điều này giúp bạn tránh thời gian xử lý không cần thiết và mức phí yêu cầu cao hơn. Để biết thông tin chi tiết, hãy xem phần Chọn thông tin cần trả về.
Cách hoạt động của tính năng Tính toán tuyến đường
Phương thức ComputeRoutes
của API Routes chấp nhận yêu cầu POST HTTP với phần nội dung yêu cầu JSON chứa thông tin chi tiết về yêu cầu. Bạn cần có nguồn gốc, đích đến, travelMode
và mặt nạ trường để chỉ định những trường cần trả về.
Ví dụ:
curl -X POST -d '{
"origin": {
"address": "1800 Amphitheatre Parkway, Mountain View, CA 94043"
},
"destination": {
"address": "Sloat Blvd &, Upper Great Hwy, San Francisco, CA 94132"
},
"travelMode": "DRIVE"
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters' \
'https://routes.googleapis.com/directions/v2:computeRoutes'
Sau đó, dịch vụ sẽ tính toán tuyến đường được yêu cầu và trả về các trường bạn đã yêu cầu.
Tài nguyên
Bảng sau đây tóm tắt các tài nguyên có sẵn thông qua phương thức Tính toán tuyến đường của API Tuyến đường, cùng với dữ liệu mà phương thức này trả về.
Tài nguyên dữ liệu |
Dữ liệu được trả về |
Định dạng trả về |
ComputeRoutes |
Trả về các tuyến đường, chặng và bước cho một tuyến đường, cùng với các tuyến đường thay thế, nếu được yêu cầu. |
JSON |
Cách sử dụng Tuyến tính toán
Các thư viện ứng dụng hiện có
Để biết danh sách các thư viện ứng dụng hiện có cho Compute Routes, hãy xem phần Thư viện ứng dụng.
Bước tiếp theo
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-06-20 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-06-20 UTC."],[],[],null,["# Compute Routes Overview\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[](/maps/documentation/routes/demo)\n\nIntroduction\n------------\n\nCompute Routes is a method in the Routes API service that accepts\nan HTTPS request and returns the ideal route between two locations.\nProvide directions with real-time traffic for transit, biking, driving,\n2-wheel motorized vehicles, or walking between multiple locations.\n\n**Need Route Matrixes?** If you are interested in a route matrix, see\n[Compute Route Matrix Overview](/maps/documentation/routes/compute-route-matrix-over).\n\n**Migrating?** If you are migrating from the Distance Matrix API (Legacy),\nsee migration instructions starting with\n[Why migrate to the Routes API](/maps/documentation/routes/migrate-routes-why).\n\nWhy use Compute Routes\n----------------------\n\nWith Compute Routes, with a wide\nrange of route details you can route your vehicles or packages according to\nyour preferences while optimizing for cost and quality.\n\nWhat you can do with Compute Routes?\n------------------------------------\n\nWith the Routes API `Compute Routes` method, you can\ndo the following things:\n\n- **Get directions for different ways to travel**, and for a single or\n multiple destinations:\n\n - Modes of transportation: transit, driving, two-wheel vehicles,\n walking, or bicycling.\n\n - A series of waypoints that you can optimize for the most efficient\n order in which to travel to them.\n\n- **Use multiple ways to specify origins, destinations, and waypoints**:\n\n - Text strings. For example: \"Chicago, IL\", \"Darwin, NT, Australia\", \"1800\n Amphitheatre Parkway, Mountain View, CA 94043\", or \"CWF6+FWX Mountain\n View, California\"\n\n - Place IDs\n\n - Latitude and longitude coordinates, optionally with vehicle heading\n\n- **Fine-tune your route options** based on your needs and goals:\n\n - Select fuel or energy-efficient routes for your vehicle's engine type:\n Diesel, Electric, Hybrid, Gas. For more information, see\n [Get an eco-friendly route](/maps/documentation/routes/eco-routes#request_an_eco-friendly_route).\n\n - Set fine-grained options for traffic calculation, letting you make\n quality versus latency trade off decisions. For details, see\n [Specify how and if to include traffic data](/maps/documentation/routes/config_trade_offs).\n\n - Set vehicle heading (direction of travel) and side-of-road information\n for waypoints to increase ETA accuracy. For details, see\n [Specify vehicle heading and side of road](/maps/documentation/routes/location_modifiers).\n\n - Specify pass-through versus terminal locations and safe stopover\n locations. For details, see [Set a stop along a route](/maps/documentation/routes/stop_over) and\n [Set a point for a route to pass through](/maps/documentation/routes/pass-through).\n\n - Request toll information, along with route distance and ETA. For\n details, see\n [Calculate toll fees for a route](/maps/documentation/routes/calculate_toll_fees).\n\n- **Control your latency and quality** by requesting only the data you need\n using a field mask, which helps you avoid unnecessary processing time and\n higher request billing rates. For details, see\n [Choose what information to return](/maps/documentation/routes/choose_fields).\n\nHow Compute Routes works\n------------------------\n\nThe Routes API `ComputeRoutes` method accepts an HTTP POST request with\na JSON request body that contains the request details. Required are an origin,\ndestination, `travelMode`, and a field mask to specify which fields to return. \n\n#### Example\n\n```json\ncurl -X POST -d '{\n \"origin\": {\n \"address\": \"1800 Amphitheatre Parkway, Mountain View, CA 94043\"\n },\n \"destination\": {\n \"address\": \"Sloat Blvd &, Upper Great Hwy, San Francisco, CA 94132\"\n },\n \"travelMode\": \"DRIVE\"\n }' \\\n -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n -H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters' \\\n 'https://routes.googleapis.com/directions/v2:computeRoutes'\n```\n\nThe service then calculates the requested route, and returns the fields you've\nrequested.\n\n### Resources\n\nThe following table summarizes the resources available through the\nRoutes API Compute Routes method,\nalong with the data it returns.\n\n\u003cbr /\u003e\n\n| Data resources | Data returned | Return format |\n|----------------------------------------------------------------------|-----------------------------------------------------------------------------------|---------------|\n| [ComputeRoutes](/maps/documentation/routes/compute_route_directions) | Returns routes, legs, and steps for a route, with alternate routes, if requested. | JSON |\n\n\u003cbr /\u003e\n\n### How to use Compute Routes\n\n|---|---------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| 1 | **Get set up** | Start with [Set up your Google Cloud project](/maps/documentation/routes/cloud-setup) and complete the setup instructions that follow. |\n| 2 | **Understand how the Routes API bills** | For information, see [Usage and billing](/maps/documentation/routes/usage-and-billing). |\n| 3 | **Compute a route and review the response** | For more information, see [Get a route](/maps/documentation/routes/compute_route_directions) and [Review the route responses](/maps/documentation/routes/understand-route-response). |\n\n### Available client libraries\n\nFor a list of the available client libraries for\nCompute Routes, see\n[Client libraries](/maps/documentation/routes/client-libraries).\n\nWhat's next\n-----------\n\n- [Get a route](/maps/documentation/routes/compute_route_directions)\n- [Available route options](/maps/documentation/routes/route-opt)\n- [Choose what information to return](/maps/documentation/routes/choose_fields)\n- [Migrate from Directions API (Legacy)](/maps/documentation/routes/migrate-routes)\n- [Migrate from the Routes API preview to GA](/maps/documentation/routes/migrate-routes-preview)"]]