我正在使用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 时,我可能会以不同的顺序获取它们,但不确定为什么其他东西被重新排序......
即使我使用航点优化,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 路线 API 文档,当航点不是中途停留时,您无法使用航点优化。
但是,我希望我的航点一定是中途停留,这样我就可以得到durationInTraffic
的估计,但我也想优化航点。
解决方案:执行 2 个请求:
- 第一个优化航点(但一定要有中途停留航点(;
- 第二个,我们使用请求 1 中优化的航点的顺序来获取通过已优化航点的路线的流量估计。