Class: GoogleMaps::Services::Directions

Inherits:
Object
  • Object
show all
Defined in:
lib/googlemaps/services/directions.rb

Overview

Performs requests to the Google Maps Directions API.

Examples:

directions = GoogleMaps::Services::Directions.new(client)
result = directions.query(origin: "Brussels", destination: {:lat => 52.520645, :lng => 13.409779})

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Directions

Returns a new instance of Directions.

Since:

  • 1.0.0



16
17
18
# File 'lib/googlemaps/services/directions.rb', line 16

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientSymbol

Returns The HTTP client.

Returns:

  • (Symbol)

    The HTTP client.

Since:

  • 1.0.0



14
15
16
# File 'lib/googlemaps/services/directions.rb', line 14

def client
  @client
end

Instance Method Details

#query(origin:, destination:, 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, traffic_model: nil) ⇒ Array, Nokogiri::XML::NodeSet

Get directions between an origin point and a destination point.

Parameters:

  • origin (String, Hash)

    The address or lat/lng hash value from which to calculate directions

  • destination (String, Hash)

    The address or lat/lng value from which to calculate directions

  • mode (String) (defaults to: nil)

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

  • waypoints (Array) (defaults to: nil)

    Specifies an array of waypoints. Waypoints alter a route by routing it through the specified location(s). A location can be a String or a lat/lng hash.

  • alternatives (TrueClass, FalseClass) (defaults to: false)

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

  • avoid (Array) (defaults to: nil)

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

  • 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, Time) (defaults to: nil)

    Specifies the desired time of departure.

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

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

  • optimize_waypoints (TrueClass, FalseClass) (defaults to: false)

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

  • transit_mode (Array) (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”.

  • traffic_model (String) (defaults to: nil)

    Specifies the predictive travel time model to use. Valid values are “best_guess” or “optimistic” or “pessimistic”. The traffic_model parameter may only be specified for requests where the travel mode is driving, and where the request includes a departure_time.

Returns:

  • (Array, Nokogiri::XML::NodeSet)

    Valid JSON or XML response.

Since:

  • 1.0.0



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/googlemaps/services/directions.rb', line 46

def query(origin:, destination:, 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, traffic_model: nil)
  params = {
      'origin' => Convert.to_latlng(origin),
      'destination' => Convert.to_latlng(destination)
  }

  if mode
    unless Constants::TRAVEL_MODES.include? mode
      raise StandardError, 'invalid travel mode.'
    end
    params['mode'] = mode
  end

  if waypoints
    waypoints = Convert.piped_location(waypoints)
    if optimize_waypoints
      waypoints = 'optimize:true|' + waypoints
    end
    params['waypoints'] = waypoints
  end

  if alternatives
    params['alternatives'] = true
  end

  if avoid
    unless ArrayBox.contains_all?(Constants::AVOID_FEATURES, avoid)
      raise StandardError, 'invalid avoid feature.'
    end
    params['avoid'] = Convert.join_array('|', avoid)
  end

  if language
    params['language'] = language
  end

  if units
    params['units'] = units
  end

  if region
    params['region'] = region
  end

  if departure_time
    params['departure_time'] = Convert.unix_time(departure_time)
  end

  if arrival_time
    params['arrival_time'] = Convert.unix_time(arrival_time)
  end

  if departure_time && arrival_time
    raise StandardError, 'should not specify both departure_time and arrival_time.'
  end

  if transit_mode
    params['transit_mode'] = Convert.join_array('|', transit_mode)
  end

  if transit_routing_preference
    params['transit_routing_preference'] = transit_routing_preference
  end

  if traffic_model
    params['traffic_model'] = traffic_model
  end

  case self.client.response_format
  when :xml
    self.client
        .request(url: '/maps/api/directions/xml', params: params)
        .xpath('//route')
  when :json
    self.client
        .request(url: '/maps/api/directions/json', params: params)
        .fetch('routes', [])
  else
    raise StandardError, 'Unsupported response format. Should be either :json or :xml.'
  end
end