google-mapsgoogle-apigoogle-api-java-clientdirectionsgoogle-directions-api

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


I am generating the 3 following Google Directions API url with the Java Client:

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

As you can see, the URLs contain the same parameters, but they are reordered (it is normal for the waypoints to be reordered since when I generate the URL I might get them in a different order, but not sure why the other stuff is reordered though...).

Even though I am using waypoints optimization, the URLs give different results.

Can someone explain why is this hapenning?

Code to make the requests:

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();

Thanks!


Solution

  • Found out the problem.

    According to Google Directions API documentation, you cannot use waypoint optimization when the waypoints are not stopovers.

    However, I wanted my waypoints to necessarily be stopovers so I can get an estimation for the durationInTraffic, but I also wanted to have waypoints optimization.

    Solution: do 2 requests: