【问题标题】:Google Maps API v3: Mulitple DirectionsRequests need different time to be answeredGoogle Maps API v3:多个方向请求需要不同的时间来回答
【发布时间】:2023-04-06 13:09:01
【问题描述】:

我想在 Google Maps API v3, GWT 2.5.1 中绘制五条不同的路线。我初始化了一个在这个类中设置其DirectionDisplayDirectionsRequest 的路由。

当我开始我的网络项目时,有时只显示我的第一条路线,有时会显示全部五个,所以我决定制作一个System.out.print(m);。

结果:

  • 01234 -> 符合预期,显示所有路线

  • 10234 -> 错误,仅显示第一条路线。

为什么 Google 会在我的第一个请求之前处理我的第二个请求?我尝试使用Thread.sleep(1000) 来确保我的请求有时间按顺序返回,还有Timer/TimerTasks,没有成功。有什么想法吗?

DirectionsService o = DirectionsService.newInstance();

for (Integer i = 0; i < 4; i++) {  //routes.size()

    final int m = i;

    final Route route = new Route("Route " + i.toString());

    route.initRoute(m, getRoutingPresenter(), adressData, addressIndex);
    //here i initialize the DirectionsRequests and its Displays, which
    //i set in this class after execution.

    o.route(directionsRequest, new DirectionsResultHandler() {

        @Override
        public void onCallback(DirectionsResult result,DirectionsStatus status) {

            if (status == DirectionsStatus.OK) {

                   System.out.print(m);
                       ...
            }
        }
    );
}
}

【问题讨论】:

    标签: java gwt google-maps-api-3


    【解决方案1】:

    Google 可以根据需要处理您的请求,您应该相应地编写代码。任何 HTTP 流量都是如此。即使远程服务器为所有请求保证固定的服务时间,Internet 也不会,您的请求可能会采用任何旧路由通过它。

    您可以修改您的处理代码以使响应顺序无关紧要,或者编写它以便等待所有响应都返回,然后自行整理顺序。

    我会推荐第一个,除非有非常具体的重要理由不这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2015-06-19
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多