Class: GoogleMaps::Services::DistanceMatrix

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

Overview

Performs requests to the Google Maps Distance Matrix API.

Examples:

distancematrix = GoogleMaps::Services::DistanceMatrix.new(client)
result = distancematrix.query(origins: ["Brussels", "Ghent"], destinations: ["Bruges"])

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ DistanceMatrix

Returns a new instance of DistanceMatrix.

Since:

  • 1.0.0



16
17
18
# File 'lib/googlemaps/services/distancematrix.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/distancematrix.rb', line 14

def client
  @client
end

Instance Method Details

#query(origins:, destinations:, mode: nil, language: nil, avoid: nil, units: nil, departure_time: nil, arrival_time: nil, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil, region: nil) ⇒ Hash, Nokogiri::XML::Document

Gets travel distance and time for a matrix of origins and destinations.

Parameters:

  • origins (Array)

    One or more locations and/or lat/lng values, from which to calculate distance and time. If you pass an address as a string, the service will geocode the string and convert it to a lat/lng coordinate to calculate directions.

  • destinations (Array)

    One or more addresses and/or lat/lng values, to which to calculate distance and time. If you pass an address as a string, the service will geocode the string and convert it to a lat/lng coordinate to calculate directions.

  • mode (String) (defaults to: nil)

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

  • language (String) (defaults to: nil)

    The language in which to return results.

  • avoid (String) (defaults to: nil)

    Indicates that the calculated route(s) should avoid the indicated features. Valid values are “tolls”, “highways” or “ferries”.

  • units (String) (defaults to: nil)

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

  • departure_time (Integer, Time, Date) (defaults to: nil)

    Specifies the desired time of departure.

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

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

  • transit_mode (Array) (defaults to: nil)

    Specifies one or more preferred modes of transit. his 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.

  • region (String) (defaults to: nil)

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

Returns:

  • (Hash, Nokogiri::XML::Document)

    Matrix of distances.

Since:

  • 1.0.0



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
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/googlemaps/services/distancematrix.rb', line 40

def query(origins:, destinations:, mode: nil, language: nil, avoid: nil, units: nil, departure_time: nil,
          arrival_time: nil, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil, region: nil)
  params = {
      'origins' => Convert.piped_location(origins),
      'destinations' => Convert.piped_location(destinations)
  }

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

  if language
    params['language'] = language
  end

  if avoid
    raise StandardError, 'Invalid route restriction.' unless Constants::AVOID_FEATURES.include? avoid
    params['avoid'] = avoid
  end

  if units
    params['units'] = units
  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

  if region
    params['region'] = region
  end

  self.client.request(url: "/maps/api/distancematrix/#{self.client.response_format}", params: params)
end