ผลิตภัณฑ์หรือฟีเจอร์นี้อยู่ในสถานะเดิม ดูข้อมูลเพิ่มเติมเกี่ยวกับระยะเดิมและวิธีย้ายข้อมูลจากบริการเดิมไปยังบริการที่ใหม่กว่าได้ที่
ผลิตภัณฑ์และฟีเจอร์เดิม
ภาพรวม Distance Matrix API
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
นักพัฒนาแอปในเขตเศรษฐกิจยุโรป (EEA)
บทนำ
Distance Matrix API (เดิม) เป็นบริการที่ยอมรับคำขอ HTTPS
ซึ่งมีต้นทางและปลายทางสำหรับรูปแบบการเดินทางที่กำหนด สำหรับ
การผสมผสานต้นทางและปลายทางแต่ละรายการ ระบบจะแสดงระยะทางและระยะเวลาในการเดินทาง
เหตุใดจึงต้องใช้ Distance Matrix API
คุณสามารถใช้ Distance Matrix API เพื่อช่วยกำหนดเส้นทางการเดินทางที่มีประสิทธิภาพมากที่สุด
ระหว่างต้นทางและปลายทางที่เป็นไปได้หลายแห่ง เช่น คนงานที่จะส่งไปยังสถานที่ทำงาน หรือคลังสินค้าที่จะส่งพัสดุ
สิ่งที่คุณทำได้ด้วย Distance Matrix API
Distance Matrix API ช่วยให้คุณระบุระยะทางและเวลาในการเดินทางสำหรับ
เมทริกซ์ของต้นทางและปลายทางได้ คุณระบุตัวเลือกได้หลายอย่าง เช่น
รูปแบบการเดินทาง เช่น การขับรถ การปั่นจักรยาน การขนส่งสาธารณะ หรือการเดิน รวมถึง
รูปแบบการขนส่งสาธารณะ เช่น รถประจำทาง รถไฟใต้ดิน รถไฟ รถราง หรือรถไฟ
Distance Matrix API ให้ข้อมูลตามเส้นทางที่แนะนำ
ระหว่างจุดเริ่มต้นและจุดสิ้นสุด คุณขอข้อมูลระยะทางประเภทต่อไปนี้ได้
- ระยะทางสำหรับโหมดการเดินทางที่เลือก
- ระยะทางเป็นกิโลเมตรหรือไมล์
- เวลาเดินทางโดยประมาณในการจราจร

วิธีการทำงานของ Distance Matrix API
Distance Matrix API ใช้ต้นทาง (จุดเริ่มต้น) และปลายทางได้ไม่จำกัดจำนวน และจะแสดงระยะทางและเวลาในการเดินทางระหว่างต้นทางแต่ละแห่งกับปลายทางที่ขอทั้งหมด โดยเริ่มจากต้นทางแรกในคำขอและดำเนินการทีละขั้นตอนไปยังต้นทางถัดไป
ตัวอย่างเช่น หากคำขอระบุ A และ B เป็นต้นทาง และ C และ D เป็นปลายทาง ระบบจะแสดงระยะทางและเวลาในการเดินทางตามลำดับนี้
- A ถึง C
- A ถึง D
- B2C
- B ถึง D
ตัวอย่างต่อไปนี้แสดงคำขอสำหรับต้นทาง 2 แห่ง ได้แก่ วอชิงตัน ดี.ซี. และบอสตัน โดยมีปลายทางเดียวคือ นิวยอร์กซิตี้ รัฐนิวยอร์ก ในรูปแบบ JSON
https://maps.googleapis.com/maps/api/distancematrix/json
?destinations=New%20York%20City%2C%20NY
&origins=Washington%2C%20DC%7CBoston
&units=imperial
&key=YOUR_API_KEY
แหล่งข้อมูล
ตารางต่อไปนี้สรุปทรัพยากรที่มีผ่าน Distance Matrix API พร้อมกับข้อมูลที่ส่งคืน
แหล่งข้อมูล |
ข้อมูลที่แสดงผล |
รูปแบบการแสดงผล |
เมทริกซ์ระยะทาง
ระบุต้นทางและปลายทางในรูปแบบรหัสสถานที่ ที่อยู่
หรือพิกัดละติจูด/ลองจิจูด
|
- ระยะทางและเวลาเดินทางระหว่างต้นทางแต่ละแห่งกับ
ปลายทางแต่ละแห่ง
- ระยะเวลาในการจราจรหากคุณระบุโหมดการขับขี่และ
เวลาออกเดินทาง
|
|
วิธีใช้ Distance Matrix API
1 |
เริ่มต้นใช้งาน |
เริ่มต้นด้วยตั้งค่าโปรเจ็กต์ Google Cloud
และทําตามวิธีการตั้งค่า
ที่ตามมาให้เสร็จสมบูรณ์ |
2 |
ลองส่งคำขอเมทริกซ์ระยะทาง |
เมื่อมีคีย์ API แล้ว คุณจะเริ่มทดสอบ Distance Matrix API ได้โดยตรงจากเบราว์เซอร์ ดูตัวอย่างได้ในคำขอตัวอย่าง
ในคู่มือเริ่มต้นใช้งาน
|
3 |
เขียนคำขอที่ซับซ้อนมากขึ้น |
ลองส่งคำขอที่ระบุต้นทางและปลายทางหลายรายการ ดูข้อมูล
เกี่ยวกับพารามิเตอร์ที่ไม่บังคับเพื่อจำกัดการคำนวณ เช่น เวลาออกเดินทางที่ระบุ
หรือข้อจำกัดของเส้นทาง ดูพารามิเตอร์ที่ไม่บังคับ
ในคู่มือคำขอและการตอบกลับ Distance Matrix
|
4 |
ทำความเข้าใจพื้นฐานของคำตอบ |
สำรวจการตอบกลับของข้อมูลเพื่อเตรียมใช้ข้อมูลเมทริกซ์ระยะทางสำหรับแอปของคุณ ดูรายละเอียดได้ที่
การตอบกลับของเมทริกซ์ระยะทาง
|
5 |
รวมข้อมูลเมทริกซ์ระยะทางไว้ในแอปของคุณเอง |
คุณใช้ข้อมูลนี้เพื่อคำนวณระยะทางและเวลาเดินทางได้
|
ไลบรารีของไคลเอ็นต์ที่พร้อมใช้งาน
เรียกใช้ API นี้ในภาษาที่คุณเลือกผ่านไลบรารีของไคลเอ็นต์ต่อไปนี้
ไคลเอ็นต์ Java, ไคลเอ็นต์ Python, ไคลเอ็นต์ Go และไคลเอ็นต์ Node.js สำหรับบริการ Google Maps เป็นไลบรารีของไคลเอ็นต์ที่ได้รับการสนับสนุนจากชุมชนและเป็นโอเพนซอร์สภายใต้สัญญาอนุญาต Apache 2.0
ดาวน์โหลดได้จาก GitHub ซึ่งคุณจะดูวิธีการติดตั้งและโค้ดตัวอย่างได้ด้วย
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe Distance Matrix API calculates travel distance and time between multiple origins and destinations, supporting various modes of transport like driving, biking, transit, or walking.\u003c/p\u003e\n"],["\u003cp\u003eThis API is ideal for optimizing logistics, such as determining efficient delivery routes or assigning workers to job sites based on travel time and distance.\u003c/p\u003e\n"],["\u003cp\u003eThe API returns data in JSON or XML format, providing distance and duration information for each origin-destination pair, including traffic estimations for driving mode.\u003c/p\u003e\n"],["\u003cp\u003eWhile it doesn't provide turn-by-turn directions, you can use the Directions API for detailed route guidance between a single origin and destination.\u003c/p\u003e\n"],["\u003cp\u003eYou can access the Distance Matrix API using client libraries for Java, Python, Go, and Node.js, allowing integration with your applications for custom solutions.\u003c/p\u003e\n"]]],["The Distance Matrix API calculates travel distance and duration between multiple origins and destinations for various transport modes like driving, biking, transit, or walking. It takes an HTTPS request specifying origins and destinations and returns data for each origin-destination pair, in a step-wise fashion. Users can customize requests with parameters like transport mode, units, and departure times, to be used for travel efficiency. The service can return distance, and estimated travel time, including traffic conditions. It supports JSON and XML return format.\n"],null,["# Distance Matrix API 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\nIntroduction\n------------\n\nThe Distance Matrix API (Legacy) is a service that accepts an HTTPS request\ncontaining origins and destinations for a given mode of transport. For each\ncombination of origin and destination, it returns travel distance and duration. \n\nWhy use the Distance Matrix API\n-------------------------------\n\nYou can use the Distance Matrix API to help determine the most efficient\ntravel routes between multiple possible origins and destinations. For\nexample, which workers to send to job sites, or from which warehouses to send\npackages.\n| This API is now in [legacy\n| mode](https://developers.google.com/maps/legacy). Use [Compute Route Matrix](/maps/documentation/routes/compute-route-matrix-over) instead.\n\nWhat you can do with the Distance Matrix API\n--------------------------------------------\n\nWith the Distance Matrix API, you can provide travel distance and time for\na matrix of origins and destinations. You can specify several options, including\nmode of transportation, such as driving, biking, transit or walking, as well as\ntransit modes, such as bus, subway, train, tram, or rail.\n\nThe Distance Matrix API provides information based on the recommended route\nbetween start and end points. You can request these kinds of distance data:\n\n- Distance for a selected travel mode\n- Distance in kilometers or miles\n- Estimated travel time in traffic\n\nHow the Distance Matrix API works\n---------------------------------\n\nThe Distance Matrix API uses any number of origins (starting points) and destinations, and returns the distance and travel time between each\norigin and all requested destinations, starting with the first origin in\nthe request and proceeding step-wise to the next.\n\nFor example, if your request specifies A and B as origins, and C and D as destinations, it returns distances and travel time in this order:\n\n- A to C\n- A to D\n- B to C\n- B to D\n\nThe following example shows a request for two origins:\nWashington, DC and Boston, with a single destination, New York City, NY, in\nJSON format: \n\n```scdoc\nhttps://maps.googleapis.com/maps/api/distancematrix/json\n ?destinations=New%20York%20City%2C%20NY\n &origins=Washington%2C%20DC%7CBoston\n &units=imperial\n &key=YOUR_API_KEY\n```\n\n### Resources\n\nThe following table summarizes the resources available through the\nDistance Matrix API along with the data it returns.\n\n\u003cbr /\u003e\n\n| Data resources | Data returned | Return format |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|---------------|\n| [Distance matrix](/maps/documentation/distance-matrix/distance-matrix) Supply origins and destinations in the form of place IDs, addresses, or latitude/longitude coordinates. | - Distance and travel time between each origin and each destination - Duration in traffic if you've specified the driving mode and a departure time | - JSON - XML |\n\n\u003cbr /\u003e\n\nHow to use the Distance Matrix API\n----------------------------------\n\n|---|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| 1 | **Get set up** | Start with [Set up your Google Cloud project](/maps/documentation/distance-matrix/cloud-setup) and complete the setup instructions that follow. |\n| 2 | **Try a distance matrix request** | Once you have an API key, you can start testing out the Distance Matrix API directly from your browser. See [Sample requests](/maps/documentation/distance-matrix/start#sample-request) in the *Get started* guide for examples. |\n| 3 | **Compose a more complex request** | Try a request that supplies multiple origins and destinations. Learn about optional parameters to constrain calculations, such as a specified departure time, or route restrictions. See [Optional parameters](/maps/documentation/distance-matrix/distance-matrix#optional-parameters) in the *Distance Matrix request and response* guide. |\n| 4 | **Understand response basics** | Explore the data responses to prepare to use distance matrix data for your app. See [Distance matrix responses](/maps/documentation/distance-matrix/distance-matrix#distance-matrix-responses) for details. |\n| 5 | **Incorporate distance matrix data into your own app!** | You can use this data to calculate travel distance and time. |\n\n### Available client libraries\n\nCall this API in the language of\nyour choice through one of the following client libraries:\n\n- [Java\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-java)\n- [Python\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python)\n- [Go Client\n for Google Maps Services](https://github.com/googlemaps/google-maps-services-go)\n- [Node.js\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-js)\n\nThe Java Client, Python Client, Go Client and Node.js Client for Google Maps\nServices are community-supported client libraries, open sourced under the\n[Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).\nDownload them from GitHub, where you can also find installation instructions and sample code.\n\nWhat's next\n-----------\n\n- **Start using the Distance Matrix** : Go to [Get Started](/maps/documentation/distance-matrix/start).\n- **Get started with sample requests and responses** : Go to [Distance Matrix\n request and response](/maps/documentation/distance-matrix/distance-matrix).\n- **Follow best practices** : Go to [Web Service Best\n Practices](/maps/documentation/distance-matrix/web-service-best-practices)."]]