'How To use DistanceMatrix Api in Spring boot Application

I have a requirement to use DestanceMatrix API to calculate the distance between origin to destination.

can anyone help me to implement this functionality in the spring boot Application?

Thanks in advance...



Solution 1:[1]

public static void distanceMatrixTest() {
    GeoApiContext context = new GeoApiContext.Builder().apiKey(GOOGLE_API_KEY_STATIC).build();

    DistanceMatrixRow[] rows = DistanceMatrixApi.newRequest(context)
            .origins(new LatLng(3.1054104954261823, 101.39172498261695))
            .destinations(new LatLng(3.0651174019721434, 101.79107501515256),
                    new LatLng(3.0362824146932934, 101.75871771663888),
                    new LatLng(3.0717816850989803, 101.75618972318607))
            .awaitIgnoreError().rows;

    if (rows.length != 0) {
        for (int i = 0; i < rows.length; i++) {

            for (int j = 0; j < rows[i].elements.length; j++) {
                System.out.println("Distance for j === " + j + " - " + rows[i].elements[j].distance.humanReadable);
                System.out.println("Distance for j === " + j + " - " + rows[i].elements[j].distance.inMeters);
                System.out.println("-----------------------------------------");
                System.out.println("Duration for j === " + j + " - " + rows[i].elements[j].duration.humanReadable);
                System.out.println("Duration for j === " + j + " - " + rows[i].elements[j].duration.inSeconds);
                System.out.println("Status for j === " + j + " - " + rows[i].elements[j].status);
                System.out.println("------------ NEXT Record ----------------");
            }

        }

    }

    // Invoke .shutdown() after your application is done making requests
    context.shutdown();

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ken