英文:
How to get the time of public transport in maps api
问题
我正在构建一个个人项目,以便熟悉API调用等内容。
我有以下函数:
public void calculateDistance(House house) {
DirectionsApiRequest apiRequest = DirectionsApi.newRequest(geoApiContext);
apiRequest.origin(new LatLng(house.getLat(), house.getLon()));
apiRequest.destination(biminghamInternationStationLonLat);
apiRequest.mode(TravelMode.TRANSIT);
apiRequest.setCallback(new com.google.maps.PendingResult.Callback<DirectionsResult>() {
@Override
public void onResult(DirectionsResult result) {
DirectionsRoute[] routes = result.routes;
System.out.println("打印结果:" + house.getUrlListing());
for(int i = 0; i < routes.length; i++) {
DirectionsRoute route = routes[i];
System.out.println(route);
}
}
@Override
public void onFailure(Throwable e) {
}
});
}
该函数的作用是获取我提供的自定义House对象的纬度和经度,然后找出乘坐公共交通工具从该位置到伯明翰国际站所需的时间(因此在apiRequest中使用了TRANSIT模式)。
但我不确定我是否使用正确?当我打开谷歌地图网站并查看从房子位置到伯明翰国际站的所需时间时,结果各不相同,大约在30-35分钟之间。但当我尝试调用上述代码时,它打印出以下内容:
[DirectionsRoute: "", 1 legs, waypointOrder=[], bounds=[52.48039080,-1.72493200, 52.45082300,-1.78392750], 1 warnings]
我不确定如何从API获取通过公共交通工具所需的时间。我正在使用Directions API,但不确定是否使用正确的API,但在查看要使用哪个API时,描述的是我需要的内容。
英文:
I am building a personal project to familiarise myself with API calling etc.
I have the following function:
public void calculateDistance(House house) {
DirectionsApiRequest apiRequest = DirectionsApi.newRequest(geoApiContext);
apiRequest.origin(new LatLng(house.getLat(), house.getLon()));
apiRequest.destination(biminghamInternationStationLonLat);
apiRequest.mode(TravelMode.TRANSIT);
apiRequest.setCallback(new com.google.maps.PendingResult.Callback<DirectionsResult>() {
@Override
public void onResult(DirectionsResult result) {
DirectionsRoute[] routes = result.routes;
System.out.println("Printing out the results for " + house.getUrlListing());
for(int i =0 ; i < routes.length; i++)
{
DirectionsRoute route = routes[i];
System.out.println(route);
}
}
@Override
public void onFailure(Throwable e) {
}
});
}
What this function does is gets the latitude and longitude of the custom House object I am providing, and essentially finds how long it takes to reach birmingham international station via public transport (hence the TRANSIT mode in the apiRequest).
But I'm not sure if I am using it correctly? When I go on google maps website and check how long it'll take for me to get to birmingham international station from the location of the house; I get results varying from 30-35 mins, okay. But when I try calling the above code it prints the following:
[DirectionsRoute: "", 1 legs, waypointOrder=[], bounds=[52.48039080,-1.72493200, 52.45082300,-1.78392750], 1 warnings]
I'm not sure how I can get the time it takes via public transport from the api. I am using the Directions API.. not sure if im using the wrong API but when looking at what API to use, this was described what I needed..
答案1
得分: 1
我看到您正在使用Java版谷歌地图服务客户端。为了了解如何使用这个库,我建议查看位于以下位置的JavaDoc文档:
https://www.javadoc.io/doc/com.google.maps/google-maps-services/latest/index.html
通过查阅JavaDoc文档,您会发现DirectionsRoute
对象包含一个DirectionsLeg[]
数组,而方向Leg又包含一个带有Duration
对象的字段。因此,您需要循环遍历路线的所有Leg,并累加Leg的持续时间,从而得到完整的路线持续时间(以秒为单位)。
关于Java客户端库中的同步调用,您可以通过调用请求的await()
方法来同步执行请求。
请查看以下基于您的代码的示例。它展示了如何同步获取公共交通路线并计算第一条路线的持续时间(以秒为单位):
import com.google.maps.GeoApiContext;
import com.google.maps.DirectionsApiRequest;
import com.google.maps.DirectionsApi;
import com.google.maps.model.DirectionsResult;
import com.google.maps.model.DirectionsRoute;
import com.google.maps.model.DirectionsLeg;
import com.google.maps.model.LatLng;
import com.google.maps.model.TravelMode;
class Main {
public static void main(String[] args) {
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("YOUR_API_KEY")
.build();
DirectionsApiRequest apiRequest = DirectionsApi.newRequest(context);
apiRequest.origin(new LatLng(41.385064,2.173403));
apiRequest.destination(new LatLng(40.416775,-3.70379));
apiRequest.mode(TravelMode.TRANSIT);
long duration = 0;
try {
DirectionsResult res = apiRequest.await();
//Loop through legs of first route and get duration
if (res.routes != null && res.routes.length > 0) {
DirectionsRoute route = res.routes[0];
if (route.legs != null) {
for (int i = 0; i < route.legs.length; i++) {
DirectionsLeg leg = route.legs[i];
duration += leg.duration.inSeconds;
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("Duration (sec): " + duration);
}
}
祝您使用愉快!
英文:
I can see that you are using the Java Client for Google Maps Services. In order to understand how to work with the library I can suggest having a look at the JavaDoc that located at
https://www.javadoc.io/doc/com.google.maps/google-maps-services/latest/index.html
Checking the JavaDoc documentation you will see that DirectionsRoute
object contains an array of DirectionsLeg[]
and the direction leg in its turn has a field with Duration
object. So you need to loop through all legs of the route and sum up leg's duration that will give you a complete route duration in seconds.
Referring to the synchronous calls in the Java client library, you can do requests synchronously calling the await()
method of the request.
Have a look at the following example that is based on your code. It shows how to get transit directions synchronously and calculate the duration in seconds for the first route
import com.google.maps.GeoApiContext;
import com.google.maps.DirectionsApiRequest;
import com.google.maps.DirectionsApi;
import com.google.maps.model.DirectionsResult;
import com.google.maps.model.DirectionsRoute;
import com.google.maps.model.DirectionsLeg;
import com.google.maps.model.LatLng;
import com.google.maps.model.TravelMode;
class Main {
public static void main(String[] args) {
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("YOUR_API_KEY")
.build();
DirectionsApiRequest apiRequest = DirectionsApi.newRequest(context);
apiRequest.origin(new LatLng(41.385064,2.173403));
apiRequest.destination(new LatLng(40.416775,-3.70379));
apiRequest.mode(TravelMode.TRANSIT);
long duration = 0;
try {
DirectionsResult res = apiRequest.await();
//Loop through legs of first route and get duration
if (res.routes != null && res.routes.length > 0) {
DirectionsRoute route = res.routes[0];
if (route.legs !=null) {
for(int i=0; i<route.legs.length; i++) {
DirectionsLeg leg = route.legs[i];
duration += leg.duration.inSeconds;
}
}
}
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("Duration (sec): " + duration);
}
}
Enjoy!
答案2
得分: 0
旅行的持续时间位于路段中:
duration 表示该路段的总持续时间,形式为以下 Duration 对象:<br>
- value 表示持续时间,以秒为单位。<br>
- text 包含持续时间的字符串表示。<br>
如果持续时间未知,这些字段可能未定义。
如果响应中有多个路段,您可以将每个路段的 value 相加以获得总持续时间。
相关问题:Google 地图 API V3 在信息窗口中显示持续时间和距离
英文:
The duration of travel is in the legs:
> duration indicates the total duration of this leg, as a Duration object of the following form:<br>
> - value indicates the duration in seconds.<br>
> - text contains a string representation of the duration.<br>
> These fields may be undefined if the duration is unknown.
If there are multiple legs in the response, you can get the total duration by adding up the value for each leg.
Related questions: Google Maps API V3 display duration and distance in info window
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论