Module: GoogleMapsService::Directions

Included in:
Client
Defined in:
lib/google_maps_service/directions.rb

Overview

Performs requests to the Google Maps Directions API.

Instance Method Summary collapse

Instance Method Details

#directions(origin: nil, destination: nil, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil) ⇒ Object

Get directions between an origin point and a destination point.

Parameters:

  • origin (String, Hash, Array) (defaults to: nil)

    The address or latitude/longitude value from which you wish to calculate directions.

  • destination (String, Hash, Array) (defaults to: nil)

    The address or latitude/longitude value from which you wish to calculate directions.

  • mode (String) (defaults to: nil)

    Specifies the mode of transport to use when calculating directions. One of “driving”, “walking”, “bicycling” or “transit”

  • waypoints (Array<String>, Array<Hash>, Array<Array>) (defaults to: nil)

    Specifies an array of waypoints. Waypoints alter a route by routing it through the specified location(s).

  • alternatives (Boolean) (defaults to: false)

    If True, more than one route may be returned in the response.

  • avoid (Array, String) (defaults to: nil)

    Indicates that the calculated route(s) should avoid the indicated features.

  • language (String) (defaults to: nil)

    The language in which to return results.

  • units (String) (defaults to: nil)

    Specifies the unit system to use when displaying results. “metric” or “imperial”

  • region (String) (defaults to: nil)

    The region code, specified as a ccTLD (“top-level domain” two-character value.

  • departure_time (Integer, DateTime) (defaults to: nil)

    Specifies the desired time of departure.

  • arrival_time (Integer, DateTime) (defaults to: nil)

    Specifies the desired time of arrival for transit directions. Note: you can’t specify both departure_time and arrival_time.

  • optimize_waypoints (Boolean) (defaults to: false)

    Optimize the provided route by rearranging the waypoints in a more efficient order.

  • transit_mode (String, Array<String>) (defaults to: nil)

    Specifies one or more preferred modes of transit. This parameter may only be specified for requests where the mode is transit. Valid values are “bus”, “subway”, “train”, “tram”, “rail”. “rail” is equivalent to [“train”, “tram”, “subway”].

  • transit_routing_preference (String) (defaults to: nil)

    Specifies preferences for transit requests. Valid values are “less_walking” or “fewer_transfers”

Returns:

  • List of routes



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/google_maps_service/directions.rb', line 39

def directions(origin: nil, destination: nil,
    mode: nil, waypoints: nil, alternatives: false, avoid: nil,
    language: nil, units: nil, region: nil, departure_time: nil,
    arrival_time: nil, optimize_waypoints: false, transit_mode: nil,
    transit_routing_preference: nil)

  params = {
    origin: _convert_waypoint(origin),
    destination: _convert_waypoint(destination)
  }

  if mode
    # NOTE(broady): the mode parameter is not validated by the Maps API
    # server. Check here to prevent silent failures.
    unless ["driving", "walking", "bicycling", "transit"].include?(mode)
      raise ArgumentError, "Invalid travel mode."
    end
    params[:mode] = mode
  end

  if waypoints
    waypoints = GoogleMapsService::Convert.as_list(waypoints)
    waypoints = waypoints.map { |waypoint| _convert_waypoint(waypoint) }
    waypoints = ["optimize:true"] + waypoints if optimize_waypoints

    params[:waypoints] = GoogleMapsService::Convert.join_list("|", waypoints)
  end

  params[:alternatives] = "true" if alternatives
  params[:avoid] = GoogleMapsService::Convert.join_list("|", avoid) if avoid
  params[:language] = language if language
  params[:units] = units if units
  params[:region] = region if region
  params[:departure_time] = GoogleMapsService::Convert.time(departure_time) if departure_time
  params[:arrival_time] = GoogleMapsService::Convert.time(arrival_time) if arrival_time

  if departure_time and arrival_time
    raise ArgumentError, "Should not specify both departure_time and arrival_time."
  end

  params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode
  params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference

  return get("/maps/api/directions/json", params)[:routes]
end