Distance Matrix API 使用入门

示例请求和响应

此示例请求华盛顿特区和 纽约州纽约市,采用 JSON 格式:

网址

https://maps.googleapis.com/maps/api/distancematrix/json
  ?destinations=New%20York%20City%2C%20NY
  &origins=Washington%2C%20DC
  &units=imperial
  &key=YOUR_API_KEY

cURL

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'

试试看!将网址输入网络浏览器中,对此请求进行测试 - 务必将 YOUR_API_KEY 替换为您的实际 API 密钥。响应会显示指定起点与终点之间的距离和时间。

了解如何构建请求网址,包括所有可用参数。

以下代码示例采用 JSON 和 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>

请参阅开发者指南,了解响应