Compute map distances using Google's Distance Matrix API. Note: To use Google's Distance Matrix API, you must first enable the API in the Google Cloud Platform Console. See register_google().
from: name of origin addresses in a data frame (vector accepted), or a data frame with from and to columns
to: name of destination addresses in a data frame (vector accepted)
mode: driving, bicycling, walking, or transit
output: amount of output
urlonly: return only the url?
override_limit: override the current query count (.google_distance_query_times)
ext: top level domain domain extension (e.g. "com", "co.nz")
inject: character string to add to the url
...: ...
Returns
a data frame (output="simple") or all of the geocoded information (output="all")
Details
if parameters from and to are specified as geographic coordinates, they are reverse geocoded with revgeocode. note that the google maps api limits to 2500 element queries a day.
Examples
## Not run: requires Google API key, see ?register_google
## basic usage########################################mapdist("waco, texas","houston, texas")# many from, single tofrom <- c("houston, texas","dallas")to <-"waco, texas"mapdist(from, to)mapdist(from, to, mode ="bicycling")mapdist(from, to, mode ="walking")# tibble of from's, vector of to's# (with a data frame, remember stringsAsFactors = FALSE)tibble("from"= c("houston","houston","dallas"),"to"= c("waco","san antonio","houston"))%>% mapdist()# distance matrixlibrary("tidyverse")c("Hamburg, Germany","Stockholm, Sweden","Copenhagen, Denmark")%>% list(., .)%>% set_names(c("from","to"))%>% cross_df()%>% mapdist()-> distances
distances
distances %>% select(from, to, km)%>% spread(from, km)## other examples######################################### many from, single to with addressesfrom <- c("1600 Amphitheatre Parkway, Mountain View, CA","3111 World Drive Walt Disney World, Orlando, FL")to <-"1600 Pennsylvania Avenue, Washington DC"mapdist(from, to)# mode = "transitfrom <-"st lukes hospital houston texas"to <-"houston zoo, houston texas"mapdist(from, to, mode ="transit")## geographic coordinates are accepted as well########################################(wh <- as.numeric(geocode("the white house, dc")))(lm <- as.numeric(geocode("lincoln memorial washington dc")))mapdist(wh, lm, mode ="walking")## End(Not run)