英文:
How to use the Google maps java client library to get distance
问题
尝试将距离矩阵 API 集成到 Java Spring Boot 应用程序中时,我遇到了由 Google 创建的 Java 客户端,但我无法理解其中的内容。
根据这里的测试,可以通过以下方式创建请求:
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();
接下来,我应该如何处理,以及如何从 API 获取结果呢?
英文:
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
答案1
得分: 0
我终于弄明白了。
客户端库适用于异步和同步请求。我需要执行同步请求,以下代码实现了此目的:
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;
数组“rows”表示预期的距离详情。每一行都表示单个起始点到各目的地的详细信息,因此如果只有一个起始点和一个目的地,则通常只会有一行。
例如,第一个起始点到第一个目的地的距离为:
System.out.println(rows[0].elements[0].distance);
通过循环可以获取所需的所有详细信息。
阅读有关row和element详情的更多信息。
英文:
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
专注分享java语言的经验与见解,让所有开发者获益!
评论