Learn R Programming

googleway (version 2.7.1)

google_directions: Google Directions

Description

The Google Maps Directions API is a service that calculates directions between locations. You can search for directions for several modes of transportation, including transit, driving, walking, or cycling.

Usage

google_directions(origin, destination, mode = c("driving", "walking",
  "bicycling", "transit"), departure_time = NULL, arrival_time = NULL,
  waypoints = NULL, optimise_waypoints = FALSE, alternatives = FALSE,
  avoid = NULL, units = c("metric", "imperial"), traffic_model = NULL,
  transit_mode = NULL, transit_routing_preference = NULL, language = NULL,
  region = NULL, key = get_api_key("directions"), simplify = TRUE,
  curl_proxy = NULL)

Arguments

origin

Origin location as either a one or two column data.frame, a list of unnamed elements, each element is either a numeric vector of lat/lon coordinates, an address string or a place_id, or a vector of a pair of lat / lon coordinates

destination

destination location as either a one or two column data.frame, a list of unnamed elements, each element is either a numeric vector of lat/lon coordinates, an address string or place_id, or a vector of a pair of lat / lon coordinates

mode

string One of 'driving', 'walking', 'bicycling' or 'transit'.

departure_time

The desired time of departure. Use either a POSIXct time since 1st January 1970, or the string 'now'. If no value is specified it defaults to Sys.time().

arrival_time

Specifies the desired time of arrival for transit requests. Use either a POSIXct time since 1st January 1970. Note you can only specify one of arrival_time or departure_time, not both. If both are supplied, departure_time will be used.

waypoints

list of waypoints, expressed as either vectors of lat/lon coordinates, or a string address to be geocoded, or an encoded polyline enclosed by enc: and :. Only available for driving, walking or bicycling modes. List elements must be named either 'stop' or 'via', where 'stop' is used to indicate a stopover for a waypoint, and 'via' will not stop at the waypoint. See https://developers.google.com/maps/documentation/directions/intro#Waypoints for details

optimise_waypoints

boolean allow the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order. (This optimization is an application of the Travelling Salesman Problem.) Travel time is the primary factor which is optimized, but other factors such as distance, number of turns and many more may be taken into account when deciding which route is the most efficient. All waypoints must be stopovers for the Directions service to optimize their route.

alternatives

logical If set to true, specifies that the Directions service may provide more than one route alternative in the response

avoid

character vector stating which features should be avoided. One of 'tolls', 'highways', 'ferries' or 'indoor'

units

string metric or imperial. Note: Only affects the text displayed within the distance field. The values are always in metric

traffic_model

string - one of 'best_guess', 'pessimistic' or 'optimistic'. Only valid with a departure time

transit_mode

vector of strings, either 'bus', 'subway', 'train', 'tram' or 'rail'. Only vaid where mode = 'transit'. Note that 'rail' is equivalent to transit_mode=c("train", "tram", "subway")

transit_routing_preference

vector of strings - one of 'less_walking' and 'fewer_transfers'. specifies preferences for transit routes. Only valid for transit directions.

language

string - specifies the language in which to return the results. See the list of supported languages: https://developers.google.com/maps/faq#languagesupport. If no langauge is supplied, the service will attempt to use the language of the domain from which the request was sent

region

string - specifies the region code, specified as a ccTLD ("top-level domain"). See region basing for details https://developers.google.com/maps/documentation/directions/intro#RegionBiasing

key

string - a valid Google Developers Directions API key

simplify

logical - TRUE indicates the returned JSON will be coerced into a list. FALSE indicates the returend JSON will be returned as a string

curl_proxy

a curl proxy object

Value

Either list or JSON string of the route between origin and destination

API use and limits

The amount of queries you can make to Google's APIs is dependent on both the service and the API you are using.

Each API has specific quotas and limits. Check Google's API documentation for details.

View your usage at the Google Cloud Console https://console.cloud.google.com/

Each API can only accept and return one request at a time. If you write a loop to make multiple API calls you should ensure you don't go over your quota / limits during the loop.

Examples

Run this code
# NOT RUN {
set_key("YOUR_GOOGLE_API_KEY")

## using lat/long coordinates
google_directions(origin = c(-37.8179746, 144.9668636),
          destination = c(-37.81659, 144.9841),
          mode = "walking")


## using address string
google_directions(origin = "Flinders Street Station, Melbourne",
         destination = "MCG, Melbourne",
         mode = "walking")


google_directions(origin = "Melbourne Airport, Australia",
         destination = "Portsea, Melbourne, Australia",
         departure_time =  Sys.time() + (24 * 60 * 60),
         waypoints = list(stop = c(-37.81659, 144.9841),
                           via = "Ringwood, Victoria"),
         mode = "driving",
         alternatives = FALSE,
         avoid = c("TOLLS", "highways"),
         units = "imperial",
         simplify = TRUE)

## using 'now' as departure time
google_directions(origin = "Flinders Street Station, Melbourne",
         destination = "MCG, Melbourne",
         departure_time = 'now')

## waypoints expressed as an encoded polyline
polyWaypoints <- encode_pl(tram_stops[1:2, c("stop_lat")], tram_stops[1:2, c("stop_lon")])
polyWaypoints <- list(via = paste0("enc:", polyWaypoints, ":"))

google_directions(origin = "Melbourne Zoo, Melbourne",
         destination = "Studley Park, Melbourne",
         waypoints = polyWaypoints)


## using bus and less walking
res <- google_directions(origin = "Melbourne Airport, Australia",
         destination = "Portsea, Melbourne, Australia",
         departure_time =  Sys.time() + (24 * 60 * 60),
         mode = "transit",
         transit_mode = "bus",
         transit_routing_preference = "less_walking",
         simplify = FALSE)

## using arrival time
res <- google_directions(origin = "Melbourne Airport, Australia",
         destination = "Portsea, Melbourne, Australia",
         arrival_time =  Sys.time() + (24 * 60 * 60),
         mode = "transit",
         transit_mode = "bus",
         transit_routing_preference = "less_walking",
         simplify = FALSE)

## return results in French
res <- google_directions(origin = "Melbourne Airport, Australia",
         destination = "Portsea, Melbourne, Australia",
         arrival_time =  Sys.time() + (24 * 60 * 60),
         mode = "transit",
         transit_mode = "bus",
         transit_routing_preference = "less_walking",
         language = "fr",
         simplify = FALSE)

# }

Run the code above in your browser using DataLab