Search code examples
rgoogle-maps-api-3google-directions-apigoogleway

Googleway not considering traffic for travel time (directions search)


Using the google_directions function from googleway package, it does not display the travel time according to google maps browser search.

It seems that it is not taking into account the traffic information.

Example on a busy street in New York.

a <- google_directions(origin = c(40.862804, -73.934743),
              destination = "212 5th Ave, New York, NY 10010, USA",
              mode = "driving",
              units = "metric",
              simplify = TRUE,
              key = MY_API)

The response at the current time:

b <- direction_steps(a)
total_time <- sum(b$duration$value)/60  # minutes
total_time                              # minutes
#[1] 26.1166

However, google maps browser displays 35 minutes for the same search time. I checked the routes and it is the same.

enter image description here

Same thing happens using mapsapi package:

c <- mp_directions(origin = c(-73.934743,40.862804),
               destination = "212 5th Ave, New York, NY 10010, USA",
                 mode = "driving",
                 key = MY_API)
total_time1 <- sum(mp_get_segments(c)$duration_s)/60 # minutes
total_time1                                          # minutes
# [1] 26.11667

Does anyone know how to incorporate traffic info into these functions? Or should I conclude google does not provide this level of information?


Solution

  • The googleway documentation for google_directions covers this. To get real-time data, you need the following:

    • a departure_time, either current using "now" or a specified future time using a POSIXct datetime.
    • traffic_model: Whether Google's guess should be best_guess, optimistic, or pessimistic.

    You asked for a historical travel time with your code, while Google Maps use the real-time travel time (likely the best_guess estimate).

    The Google documentation states:

    best_guess (default) indicates that the returned duration_in_traffic should be the best estimate of travel time given what is known about both historical traffic conditions and live traffic. Live traffic becomes more important the closer the departure_time is to now.

    Try this instead:

    a <- google_directions(
        origin = c(40.862804, -73.934743),
        destination = "212 5th Ave, New York, NY 10010, USA",
        mode = "driving",
        units = "metric",
        simplify = TRUE,
        key = MY_API,
        departure_time = "now",
        traffic_model = "best_guess")
    
    # Travel time in seconds
    travel_time <- a$routes$legs[[1]]$duration_in_traffic$value