1

Am trying to integrate distance matrix API into a Java spring boot application, I came across the java client created by Google but I can't get my way through it. I tried looking at the tests here but I still can't understand how to use

According to the test, a request is created by:

DistanceMatrixApi.newRequest(sc.context)
      .origins(new LatLng(-31.9522, 115.8589), new LatLng(-37.8136, 144.9631))
      .destinations(new LatLng(-25.344677, 131.036692), new LatLng(-13.092297, 132.394057))
      .awaitIgnoreError();

What do I do from here, how do I get the results from the API

Enock Lubowa
  • 679
  • 8
  • 12

1 Answers1

0

I finally figured it out. The client library caters to both asynchronous and synchronous requests. I needed to perform synchronous requests and this did the work:

DistanceMatrixRow[] rows = DistanceMatrixApi.newRequest(context)
    .origins(new LatLng(-31.9522, 115.8589), new LatLng(-37.8136, 144.9631))
    .destinations(new LatLng(-25.344677, 131.036692), new LatLng(-13.092297, 132.394057))
    .awaitIgnoreError()
    .rows;

The rows array represents the distance details as expected. Each row represents details of a single origin to each of the destinations, so you'll typically have one row if it's just one origin and one destination. An example the distance of the first origin to the first destination is:

System.out.println(rows[0].elements[0].distance);

With the help of loops, you can get all the details you want. Read more information about the details of the row and element

Enock Lubowa
  • 679
  • 8
  • 12