谷歌方向API--同样的请求有不同的结果,但参数重新排序。

问题描述 投票:-2回答:1

我正在用Java客户端生成以下3个Google Directions 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时,我可能会得到不同的顺序,但不确定为什么其他东西被重新排序...)。

尽管我使用了航点优化,但URLs给出的结果却不同。

谁能解释一下为什么会出现这种情况?

做出请求的代码。

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-maps google-api google-api-java-client directions google-direction
1个回答
0
投票

发现了问题。

根据 谷歌方向API文档不能 使用 航点优化 当航点 不是中途停留.

然而, 我想让我的航点一定是中途站 所以我可以得到一个估计的 durationInTraffic, 但我也想对航点进行优化。.

解决办法: 。 做2个请求。

  • 第一个请求是优化航点(但一定要有中途停留的航点)。
  • 第二个请求是利用请求1中优化过的航点的顺序来获取通过已经优化过的航点的路线的流量估计。
© www.soinside.com 2019 - 2024. All rights reserved.