Class: GoogleMaps::Services::Roads

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/googlemaps/services/roads.rb

Overview

Performs requests to the Google Maps Roads API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Roads

Returns a new instance of Roads.



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

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientSymbol

Returns the HTTP client.

Returns:

  • (Symbol)

    the HTTP client.



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

def client
  @client
end

Instance Method Details

#nearest_roads(points:) ⇒ Array

Find the closest road segments for each point. Takes up to 100 independent coordinates, and returns the closest road segment for each point. The points passed do not need to be part of a continuous path.

Parameters:

  • points (Array)

    The points for which the nearest road segments are to be located.

Returns:

  • (Array)

    An array of snapped points.



81
82
83
84
85
86
87
88
# File 'lib/googlemaps/services/roads.rb', line 81

def nearest_roads(points:)
  params = {'points' => Convert.piped_location(points)}

  self.client
      .request(url: '/v1/nearestRoads', params: params, base_url: Constants::ROADS_BASE_URL, accepts_clientid: false,
               extract_body: lambda(&method(:_roads_extract)))
      .fetch('snappedPoints', [])
end

#snap_to_roads(path:, interpolate: false) ⇒ Array

Snaps a path to the most likely roads travelled. Takes up to 100 GPS points collected along a route, and returns a similar set of data with the points snapped to the most likely roads the vehicle was traveling along.

Parameters:

  • path (Array)

    The path to be snapped.

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

    Whether to interpolate a path to include all points forming the full road-geometry. When true, additional interpolated points will also be returned, resulting in a path that smoothly follows the geometry of the road, even around corners and through tunnels. Interpolated paths may contain more points than the original path.

Returns:

  • (Array)

    Array of snapped points.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/googlemaps/services/roads.rb', line 30

def snap_to_roads(path:, interpolate: false)
  params = {'path' => Convert.piped_location(path)}

  if interpolate
    params['interpolate'] = 'true'
  end

  self.client
      .request(url: '/v1/snapToRoads', params: params, base_url: Constants::ROADS_BASE_URL, accepts_clientid: false,
               extract_body: lambda(&method(:_roads_extract)))
      .fetch('snappedPoints', [])
end

#snapped_speed_limits(path:) ⇒ Hash

Returns the posted speed limit (in km/h) for given road segments. The provided points will first be snapped to the most likely roads the vehicle was traveling along.

Parameters:

  • path (Array)

    The path of points to be snapped.

Returns:

  • (Hash)

    Hash with an array of speed limits and an array of the snapped points.



66
67
68
69
70
71
72
# File 'lib/googlemaps/services/roads.rb', line 66

def snapped_speed_limits(path:)
  params = {'path' => Convert.piped_location(path)}

  self.client
      .request(url: '/v1/speedLimits', params: params, base_url: Constants::ROADS_BASE_URL,
               accepts_clientid: false, extract_body: lambda(&method(:_roads_extract)))
end

#speed_limits(place_ids:) ⇒ Array

Returns the posted speed limit (in km/h) for given road segments.

Parameters:

  • place_ids (Array)

    The Place ID of the road segment. Place IDs are returned by the snap_to_roads function. You can pass up to 100 Place IDs.

Returns:

  • (Array)

    Array of speed limits.

Raises:

  • (StandardError)


49
50
51
52
53
54
55
56
57
58
# File 'lib/googlemaps/services/roads.rb', line 49

def speed_limits(place_ids:)
  raise StandardError, "#{__method__.to_s} expected an Array for place_ids." unless place_ids.is_a? Array

  params = {'placeId' => place_ids}

  self.client
      .request(url: '/v1/speedLimits', params: params, base_url: Constants::ROADS_BASE_URL, accepts_clientid: false,
               extract_body: lambda(&method(:_roads_extract)))
      .fetch('speedLimits', [])
end