Search code examples
rgoogle-mapsgoogle-maps-api-3google-distancematrix-api

Gmapsdistance function not accepting departure times


I am attempting to give a series of routes to gmapsdistance function. This has previously worked fine with other routes but is not accepting the departure_time arguments with this combination of lat and long.

I have attempted simply entering in a future time in character format ("20:00:00") but get the same error.

route_1 <- c(c(57.14748,-2.0954), c(51.12788,-4.25714))
route_2 <- c(c(55.81875,-4.02555), c(51.4721,-0.45273))
route_3 <- c(c(54.96566,-5.0152), c(55.86568,-4.25714))
route_4 <- c(c(51.12788,-4.25714), c(51.38867,0.193838))
route_5 <- c(c(55.86568,-4.25714), c(51.12788,-4.25714))
route_6 <- c(c(51.4721,-0.45273), c(51.12788,-4.25714))

result <- gmapsdistance(origin = route_6[[1]], route_6[[2]],
                        destination = route_6[[3]], route_6[[4]],
                        traffic_model = "pessimistic",
                        mode = "driving", 
                        dep_date = as.character(Sys.Date()), 
                        dep_time = as.character(Sys.time() + 60*10),
                        key=key,
                        combinations = "all",
                        avoid = "")``

Error received is strangely:

The departure time has to be some time in the future!

Solution

  • This is not related to setting departure_time in the past like the error indicates. It actually looks like it's stemming from an invalid format for origin & destination and for dep_time.

    According to the library's docs, the coordinates should be formatted as follows:

    "38.1621328+24.0029257"
    

    And departure time should only contain a time, not a date+time. E.g.:

    "20:40:00"
    

    Please try running the full code below (with your API key):

    library("gmapsdistance")
    
    set.api.key("AIza...")
    
    route_6 <- c(c("51.4721+-0.45273"), c("51.12788+-4.25714"))
    
    results = gmapsdistance(origin = route_6[[1]],
                            destination = route_6[[2]],
                            traffic_model = "pessimistic",
                            mode = "driving", 
                            dep_date = as.character(Sys.Date()), 
                            dep_time = as.character(format(Sys.time() + 60*10, "%H:%M:%S")),
                            key=get.api.key(),
                            combinations = "all",
                            avoid = "")
    results
    

    Which for me as of now it returns:

    $Time
    [1] 16842
    
    $Distance
    [1] 337527
    
    $Status
    [1] "OK"
    

    Hope this helps!