Yêu cầu và phản hồi mẫu
Ví dụ này yêu cầu dữ liệu ma trận khoảng cách giữa Washington, DC và New York City, NY, ở định dạng JSON:
URL
https://maps.googleapis.com/maps/api/distancematrix/json ?destinations=New%20York%20City%2C%20NY &origins=Washington%2C%20DC &units=imperial &key=YOUR_API_KEY
URL
curl -L -X GET 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=YOUR_API_KEY'
JavaScript
var axios = require('axios'); var config = { method: 'get', url: 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=YOUR_API_KEY', headers: { } }; axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Python
import requests url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=YOUR_API_KEY" payload={} headers = {} response = requests.request("GET", url, headers=headers, data=payload) print(response.text)
Java
OkHttpClient client = new OkHttpClient().newBuilder() .build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=YOUR_API_KEY") .method("GET", body) .build(); Response response = client.newCall(request).execute();
Ruby
require "uri" require "net/http" url = URI("https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=YOUR_API_KEY") https = Net::HTTP.new(url.host, url.port) https.use_ssl = true request = Net::HTTP::Get.new(url) response = https.request(request) puts response.read_body
Go
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington,%20DC&destinations=New%20York%20City,%20NY&units=imperial&key=YOUR_API_KEY" method := "GET" client := &http.Client { } req, err := http.NewRequest(method, url, nil) if err != nil { fmt.Println(err) return } res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }
Người đưa thư
Thông số kỹ thuật OpenAPI cũng có sẵn dưới dạng bộ sưu tập Postman.
Chạy trong PostmanHãy làm thử! Hãy kiểm tra yêu cầu này bằng cách nhập URL vào trình duyệt web của bạn. Hãy nhớ thay thế YOUR_API_KEY
bằng khoá API thực tế của bạn.
Phản hồi cho biết khoảng cách và thời lượng giữa điểm khởi hành và điểm đến đã chỉ định.
Tìm hiểu cách tạo URL yêu cầu, bao gồm tất cả các tham số có sẵn.
Mã mẫu này ở định dạng JSON và XML:
JSON
{ "destination_addresses": ["New York, NY, USA"], "origin_addresses": ["Washington, DC, USA"], "rows": [ { "elements": [ { "distance": { "text": "228 mi", "value": 367654 }, "duration": { "text": "3 hours 55 mins", "value": 14078 }, "status": "OK", }, ], }, ], "status": "OK", }
XML
<DistanceMatrixResponse> <status>OK</status> <origin_address>Washington, DC, USA</origin_address> <destination_address>New York, NY, USA</destination_address> <row> <element> <status>OK</status> <duration> <value>14078</value> <text>3 hours 55 mins</text> </duration> <distance> <value>367654</value> <text>228 mi</text> </distance> </element> </row> </DistanceMatrixResponse>
Thông số kỹ thuật OpenAPI
Thông số kỹ thuật của OpenAPI được phát hành cho API này và được cung cấp trên GitHub.
Nhận thông số kỹ thuật OpenAPIBộ sưu tập bưu thiếp
Thông số kỹ thuật OpenAPI cũng có sẵn dưới dạng bộ sưu tập Postman.
Chạy trong PostmanHãy xem hướng dẫn dành cho nhà phát triển để tìm hiểu câu trả lời.