Wypróbuj nową wersję funkcji macierzy odległości z interfejsem Routes API.

Pierwsze kroki z interfejsem DISTANCE Matrix API

Przykładowe żądanie i odpowiedź

W tym przykładzie żądanie danych macierzy odległości między Waszyngtonem a Nowym Jorku w stanie Nowy Jork w formacie 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))
}

Postman

Specyfikacja OpenAPI jest również dostępna jako kolekcja Postman.

Uruchom w Postman

Wypróbuj Przetestuj to żądanie, wpisując adres URL w przeglądarce – zastąp YOUR_API_KEY faktycznym kluczem interfejsu API. Odpowiedź pokazuje odległość i czas trwania między określonymi źródłami i miejscami docelowymi.

Dowiedz się, jak tworzyć adresy URL żądań, w tym wszystkie dostępne parametry.

Ten przykładowy kod jest w formacie JSON i 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>

Specyfikacja OpenAPI

Specyfikacja OpenAPI została opublikowana dla tego interfejsu API i dostępna w GitHub.

Pobierz specyfikację OpenAPI

Kolekcja Postman

Specyfikacja OpenAPI jest również dostępna jako kolekcja Postman.

Uruchom w Postman

Przeczytaj przewodnik dla programistów, aby dowiedzieć się, jak odpowiedzieć.