Module: GoogleMapsService::Apis::Roads

Included in:
Client
Defined in:
lib/google_maps_service/apis/roads.rb

Overview

Performs requests to the Google Maps Roads API.

Constant Summary collapse

ROADS_BASE_URL =

Base URL of Google Maps Roads API

"https://roads.googleapis.com"

Instance Method Summary collapse

Instance Method Details

#nearest_roads(points) ⇒ Array

Returns the nearest road segments for provided points. The points passed do not need to be part of a continuous path.

Examples:

Single point snap

results = client.nearest_roads([40.714728, -73.998672])

Multi points snap

points = [
    [-33.8671, 151.20714],
    [-33.86708, 151.20683000000002],
    [-33.867070000000005, 151.20674000000002],
    [-33.86703, 151.20625]
]
results = client.nearest_roads(points)

Parameters:

  • points (Array)

    The points to be used for nearest road segment lookup. Array of latitude/longitude pairs which do not need to be a part of continuous part. Takes up to 100 independent coordinates, and returns the closest road segment for each point.

Returns:

  • (Array)

    Array of snapped points.



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/google_maps_service/apis/roads.rb', line 128

def nearest_roads(points)
  points = GoogleMapsService::Convert.waypoints(points)

  params = {
    points: points
  }

  return get('/v1/nearestRoads', params,
             base_url: ROADS_BASE_URL,
             accepts_client_id: false,
             custom_response_decoder: method(:extract_roads_body))[: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.

Examples:

Single point snap

results = client.snap_to_roads([40.714728, -73.998672])

Multi points snap

path = [
    [-33.8671, 151.20714],
    [-33.86708, 151.20683000000002],
    [-33.867070000000005, 151.20674000000002],
    [-33.86703, 151.20625]
]
results = client.snap_to_roads(path, interpolate: true)

Parameters:

  • path (Array)

    The path to be snapped. Array of latitude/longitude pairs.

  • interpolate (Boolean) (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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google_maps_service/apis/roads.rb', line 38

def snap_to_roads(path, interpolate: false)
  path = GoogleMapsService::Convert.waypoints(path)

  params = {
    path: path
  }

  params[:interpolate] = 'true' if interpolate

  return get('/v1/snapToRoads', params,
             base_url: ROADS_BASE_URL,
             accepts_client_id: false,
             custom_response_decoder: method(:extract_roads_body))[: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.

Examples:

Multi points snap

path = [
    [-33.8671, 151.20714],
    [-33.86708, 151.20683000000002],
    [-33.867070000000005, 151.20674000000002],
    [-33.86703, 151.20625]
]
results = client.snapped_speed_limits(path)

Parameters:

  • path (Hash, Array)

    The path of points to be snapped. A list of (or single) latitude/longitude tuples.

Returns:

  • (Hash)

    A hash with both a list of speed limits and a list of the snapped points.



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/google_maps_service/apis/roads.rb', line 94

def snapped_speed_limits(path)
  path = GoogleMapsService::Convert.waypoints(path)

  params = {
    path: path
  }

  return get('/v1/speedLimits', params,
             base_url: ROADS_BASE_URL,
             accepts_client_id: false,
             custom_response_decoder: method(:extract_roads_body))
end

#speed_limits(place_ids) ⇒ Array

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

Examples:

Multi places snap

place_ids = [
  'ChIJ0wawjUCuEmsRgfqC5Wd9ARM',
  'ChIJ6cs2kkCuEmsRUfqC5Wd9ARM'
]
results = client.speed_limits(place_ids)

Parameters:

  • place_ids (String, Array<String>)

    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.



66
67
68
69
70
71
72
73
# File 'lib/google_maps_service/apis/roads.rb', line 66

def speed_limits(place_ids)
  params = GoogleMapsService::Convert.as_list(place_ids).map { |place_id| ['placeId', place_id] }

  return get('/v1/speedLimits', params,
             base_url: ROADS_BASE_URL,
             accepts_client_id: false,
             custom_response_decoder: method(:extract_roads_body))[:speedLimits]
end