我正在用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();
谢谢!
发现了问题。
根据 谷歌方向API文档你 不能 使用 航点优化 当航点 不是中途停留.
然而, 我想让我的航点一定是中途站 所以我可以得到一个估计的 durationInTraffic
, 但我也想对航点进行优化。.
解决办法: 。 做2个请求。