trek: Grab a trek from Google

trekR Documentation

Grab a trek from Google

Description

Sequence treks (latitude-longitude sequences following ordinary paths, e.g. roads) between two locations using the Google Directions API. Note: To use Google's Directions API, you must first enable the API in the Google Cloud Platform Console. See register_google().

Usage

trek(
  from,
  to,
  mode = c("driving", "walking", "bicycling", "transit"),
  output = c("simple", "all"),
  alternatives = FALSE,
  units = "metric",
  urlonly = FALSE,
  override_limit = FALSE,
  ext = "com",
  inject = "",
  ...
)

Arguments

from

name of origin addresses in a data frame

to

name of destination addresses in a data frame

mode

driving, bicycling, walking, or transit

output

amount of output ("simple" or "all")

alternatives

should more than one route be provided?

units

"metric"

urlonly

return only the url?

override_limit

override the current query count

ext

domain extension (e.g. "com", "co.nz")

inject

character string to add to the url

...

...

Value

a tibble

Author(s)

David Kahle david@kahle.io with the key decoding algorithm due to Stack Overflow user akhmed

See Also

https://developers.google.com/maps/documentation/directions/, https://stackoverflow.com/questions/30270011/ggmap-route-finding-doesnt-stay-on-roads/, route(), routeQueryCheck(), register_google()

Examples


## Not run:  requires Google API key, see ?register_google

## basic usage
########################################

from <- "houston, texas"
to <- "waco, texas"

(route_df <- route(from, to, structure = "route"))
(trek_df  <-  trek(from, to, structure = "route"))

qmap("college station, texas", zoom = 8) +
  geom_path(
    aes(x = lon, y = lat),  colour = "red",
    size = 1.5, alpha = .5,
    data = route_df, lineend = "round"
  ) +
  geom_path(
    aes(x = lon, y = lat),  colour = "blue",
    size = 1.5, alpha = .5,
    data = trek_df, lineend = "round"
  )



from <- "rice university houston texas"
to <- "1001 Bissonnet St, Houston, TX 77005"
trek_df <- trek(from, to)
qmplot(lon, lat, data = trek_df, geom = "path", maptype = "terrain",
  color = I("red"), size = I(2), alpha = I(.5)
)

trek_df <- trek(from, to, mode = "walking")
qmplot(lon, lat, data = trek_df, geom = "path", maptype = "terrain",
  color = I("red"), size = I(2), alpha = I(.5)
)

trek_df <- trek(from, to, mode = "transit")
qmplot(lon, lat, data = trek_df, geom = "path", maptype = "terrain",
  color = I("red"), size = I(2), alpha = I(.5)
)



## neat faceting example
########################################

from <- "houston, texas"; to <- "waco, texas"
trek_df <- trek(from, to, alternatives = TRUE)

qmplot(lon, lat, data = trek_df, geom = "path",
  color = route, size = I(2), maptype = "terrain",
  alpha = I(.5)
)

qmplot(lon, lat, data = trek_df, geom = "path",
  color = route, size = I(2), maptype = "terrain",
  zoom = 8
) + facet_grid(. ~ route)





## End(Not run)


dkahle/ggmap documentation built on March 3, 2024, 8:19 a.m.