Google 方向 API - 相同请求的不同结果,但参数重新排序

Google Directions API - different results for same request but with reordered parameters

我正在使用 Java 客户端生成 3 个遵循 Google 方向 API url:

Result -> duration: 11:40 & durationInTraffic: 12:08 -> 
/maps/api/directions/json?key=USE_YOUR_QEY&mode=driving&origin=45.41171300%2C22.21830300&destination=45.41171300%2C22.21830300&traffic_model=pessimistic&departure_time=1591716600&waypoints=optimize%3Atrue%7Cvia%3A44.51653700%2C22.35241300%7Cvia%3A44.63441300%2C22.65520500%7Cvia%3A44.72205900%2C22.39428200%7Cvia%3A45.60770700%2C22.94924000%7Cvia%3A44.54049800%2C22.77601700

Result -> duration: 11:17 & durationInTraffic: 11:52 -> 
/maps/api/directions/json?key=USE_YOUR_QEY&mode=driving&origin=45.41171300%2C22.21830300&destination=45.41171300%2C22.21830300&traffic_model=pessimistic&departure_time=1591716600&waypoints=optimize%3Atrue%7Cvia%3A44.72205900%2C22.39428200%7Cvia%3A44.63441300%2C22.65520500%7Cvia%3A44.54049800%2C22.77601700%7Cvia%3A45.60770700%2C22.94924000%7Cvia%3A44.51653700%2C22.35241300

Result -> duration: 08:20 & durationInTraffic: 08:46 -> 
/maps/api/directions/json?key=USE_YOUR_QEY&mode=driving&origin=45.41171300%2C22.21830300&destination=45.41171300%2C22.21830300&traffic_model=pessimistic&waypoints=optimize%3Atrue%7Cvia%3A44.51653700%2C22.35241300%7Cvia%3A44.54049800%2C22.77601700%7Cvia%3A44.63441300%2C22.65520500%7Cvia%3A44.72205900%2C22.39428200%7Cvia%3A45.60770700%2C22.94924000&departure_time=1591716600

如您所见,URL包含相同的参数,但它们被重新排序(自从我生成 URL 后,waypoints 被重新排序是正常的我可能会以不同的顺序得到它们,但不确定为什么其他东西会重新排序......)。

即使我使用 waypoints 优化,URL 也会给出不同的结果。

有人可以解释为什么会这样吗?

发出请求的代码:

GeographicPoint origin = route.getRoute().get(0);
GeographicPoint destination = route.getRoute().get(route.getRoute().size() - 1);

List<Waypoint> waypoints = new ArrayList<>();
route.getRoute().subList(1, route.getRoute().size() - 1).forEach(waypoint -> {
waypoints.add(new Waypoint(new LatLng(waypoint.getLatitude(), waypoint.getLongitude()), false));

DirectionsResult result = DirectionsApi.newRequest(geoApiContext)
                        .origin(new LatLng(origin.getLatitude(), origin.getLongitude()))
                        .destination(new LatLng(destination.getLatitude(), destination.getLongitude()))
                        .waypoints(waypoints.toArray(new Waypoint[0]))
                        .optimizeWaypoints(true)

                        .mode(TravelMode.DRIVING)
                        .trafficModel(TrafficModel.PESSIMISTIC)
                        .departureTime(departureTime)
                        .await();

谢谢!

找到问题了。

根据 Google Directions API documentation,当 waypoints 不是中途停留.

但是,我希望我的 waypoints 一定是中途停留,这样我就可以估计 durationInTraffic但是我还想有 waypoints 优化.

解决方案: 执行 2 个请求:

  • 第一个优化waypoints(但一定要中途停留waypoints);
  • 第二个,我们使用请求 1 中优化的 waypoints 的顺序来获得经过已优化的 waypoints 的路线的流量估计。