简体   繁体   中英

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...

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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM