Method: GoogleMapsService::Apis::Roads#snap_to_roads

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

#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.

[View source]

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