【问题标题】:How to get the time of public transport in maps api如何在maps api中获取公共交通时间
【发布时间】:2020-10-24 14:15:49
【问题描述】:

我正在构建一个个人项目以熟悉 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("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) {

        }
    });
}

此函数的作用是获取我提供的自定义 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 时,这描述了我需要的内容..

【问题讨论】:

    标签: java google-maps google-api google-api-client


    【解决方案1】:

    我可以看到您正在使用Java Client for Google Maps Services。为了了解如何使用该库,我建议您查看位于

    的 JavaDoc

    https://www.javadoc.io/doc/com.google.maps/google-maps-services/latest/index.html

    查看 JavaDoc 文档,您将看到 DirectionsRoute 对象包含一个 DirectionsLeg[] 数组,而方向腿又具有一个带有 Duration 对象的字段。因此,您需要遍历路线的所有航段并总结航段的持续时间,这将为您提供以秒为单位的完整路线持续时间。

    参考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);
      }
    } 
    

    享受吧!

    【讨论】:

      【解决方案2】:

      旅行时长在legs

      duration 表示此段的总持续时间,作为以下形式的 Duration 对象:

      • 表示持续时间,以秒为单位。
      • text 包含持续时间的字符串表示形式。
        如果持续时间未知,则这些字段可能未定义。

      如果响应中有多个分支,您可以通过将每个分支的值相加来获得总持续时间。

      相关问题:Google Maps API V3 display duration and distance in info window

      【讨论】:

      • 谢谢!也是一个快速的方法,我的问题中的代码 sn-p 中的方法“setCallback”是非阻塞的,即我相信它启动了它自己的线程。我需要它被阻塞,因为前面的代码依赖于结果。关于我如何防止代码继续前进直到它得到持续时间的结果的任何建议?
      • 服务调用是异步的,使用promise或者使用回调函数中的数据何时/何地可用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多