Module: GoogleMapsService::Apis::Places

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

Overview

Performs requests to the Google Maps Places API.

Instance Method Summary collapse

Instance Method Details

#place(place_id, language: nil) ⇒ Object

Comprehensive details for an individual place.

@param place_id A textual identifier that uniquely identifies a

place, returned from a Places search.

@param language The language in which to return results. @return Hash with the following keys:

result: dict containing place details
html_attributions: set of attributions which must be displayed


95
96
97
98
99
# File 'lib/google_maps_service/apis/places.rb', line 95

def place(place_id, language: nil)
  params = {placeid: place_id}
  params[:language] = language if language
  get("/maps/api/place/details/json", params)
end

#places(query, location: nil, radius: nil, language: nil, min_price: nil, max_price: nil, open_now: false, type: nil, page_token: nil) ⇒ Hash

Places search.

Parameters:

  • query (String)

    The text string on which to search, for example: “restaurant”.

  • location (String, Hash, Array) (defaults to: nil)

    The latitude/longitude value for which you wish to obtain the closest, human-readable address.

  • radius (Integer) (defaults to: nil)

    Distance in meters within which to bias results.

  • language (String) (defaults to: nil)

    The language in which to return results.

  • min_price (Integer) (defaults to: nil)

    Restricts results to only those places with no less than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).

  • max_price (Integer) (defaults to: nil)

    Restricts results to only those places with no greater than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).

  • open_now (Boolean) (defaults to: false)

    Return only those places that are open for business at the time the query is sent.

  • type (String) (defaults to: nil)

    Restricts the results to places matching the specified type. The full list of supported types is available here: developers.google.com/places/supported_types

  • page_token (String) (defaults to: nil)

    Token from a previous search that when provided will returns the next page of results for the same search.

Returns:

  • (Hash)

    Hash with the following keys: results: list of places html_attributions: set of attributions which must be displayed next_page_token: token for retrieving the next page of results



30
31
32
33
34
35
36
# File 'lib/google_maps_service/apis/places.rb', line 30

def places(query, location: nil, radius: nil, language: nil, min_price: nil,
  max_price: nil, open_now: false, type: nil, page_token: nil)

  _places("text", query: query, location: location, radius: radius,
    language: language, min_price: min_price, max_price: max_price,
    open_now: open_now, type: type, page_token: page_token)
end

#places_nearby(location: nil, radius: nil, keyword: nil, language: nil, min_price: nil, max_price: nil, name: nil, open_now: false, rank_by: nil, type: nil, page_token: nil) ⇒ Hash

Performs nearby search for places.

Parameters:

  • location (String, Hash, Array) (defaults to: nil)

    The latitude/longitude value for which you wish to obtain the closest, human-readable address.

  • radius (Integer) (defaults to: nil)

    Distance in meters within which to bias results.

  • keyword (String) (defaults to: nil)

    A term to be matched against all content that Google has indexed for this place.

  • language (String) (defaults to: nil)

    The language in which to return results.

  • min_price (Integer) (defaults to: nil)

    Restricts results to only those places with no less than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).

  • max_price (Integer) (defaults to: nil)

    Restricts results to only those places with no greater than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).

  • name (String, Array) (defaults to: nil)

    One or more terms to be matched against the names of places.

  • open_now (Boolean) (defaults to: false)

    Return only those places that are open for business at the time the query is sent.

  • rank_by (String) (defaults to: nil)

    Specifies the order in which results are listed. Possible values are: prominence (default), distance

  • type (String) (defaults to: nil)

    Restricts the results to places matching the specified type. The full list of supported types is available here: developers.google.com/places/supported_types

  • page_token (String) (defaults to: nil)

    Token from a previous search that when provided will returns the next page of results for the same search.

Returns:

  • (Hash)

    Hash with the following keys: status: status code results: list of places html_attributions: set of attributions which must be displayed next_page_token: token for retrieving the next page of results



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/google_maps_service/apis/places.rb', line 68

def places_nearby(location: nil, radius: nil, keyword: nil, language: nil,
  min_price: nil, max_price: nil, name: nil, open_now: false,
  rank_by: nil, type: nil, page_token: nil)
  if rank_by == "distance"
    if !(keyword || name || type)
      raise ArgumentError, "either a keyword, name, or type arg is " \
        "required when rank_by is set to distance"
    elsif radius
      raise ArgumentError, "radius cannot be specified when rank_by " \
        "is set to distance"
    end
  end

  _places("nearby", location: location, radius: radius,
    keyword: keyword, language: language, min_price: min_price,
    max_price: max_price, name: name, open_now: open_now,
    rank_by: rank_by, type: type, page_token: page_token)
end

#places_photo(photo_reference, max_width: nil, max_height: nil) ⇒ Object

Photo URL from the Places API.

@param photo_reference A string identifier that uniquely

identifies a photo, as provided by either a Places search or Places
detail request.

@param max_width Specifies the maximum desired width, in pixels. @param max_height Specifies the maximum desired height, in pixels. @return String URL of the photo or nil upon error.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/google_maps_service/apis/places.rb', line 109

def places_photo(photo_reference, max_width: nil, max_height: nil)
  unless max_width || max_height
    raise ArgumentError, "a max_width or max_height arg is required"
  end

  params = {photoreference: photo_reference}

  params[:maxwidth] = max_width if max_width
  params[:maxheight] = max_height if max_height

  image_response_decoder = ->(response) {
    response["location"]
  }

  get("/maps/api/place/photo", params,
    custom_response_decoder: image_response_decoder)
end