Ademar Tutor
Ruby on Rails Development
3 min readAug 3, 2016

--

google_places gem

Does your startup involve searching for and retrieving rich information about local businesses and points of interest? Does you startup need autocomplete, providing type-ahead location-based predictions like the search on Google Maps? Is your startup similar to Expedia or Foodspotting?

If you answered yes to either one of those question, then you should be using the Google Places API. With Google Places API, you pull rich information about places on specific locations.

Your startup is using Ruby on Rails? Your in luck, you can using google_places gem, a gem that servers as a ruby wrapper for the Google Places API.

Get a Google API Key

You need a google api key to use the Google Places. Visit code.google.com/apis/console and follow instructions on how to get an api key. Make sure you activate the google places api.

Setting up the gem

Add this to your Gemfile: gem ‘google_places’

Then bundle install

Now you can create a new client instances by doing this:

@client = GooglePlaces::Client.new(API_KEY)

API result

Each API method returns a GooglePlaces::Spot or a collection of those. A spot contains these attributes:

  • reference: a token to query the Google Places API for more details about the spot
  • vicinity: the street or neighborhood of the spot
  • lat: the latitude of the spot
  • lng: the longitude of the spot
  • name: the name of the spot
  • icon: a URL to the icon of this spot
  • types: array of feature types describing the spot, see list of supported types
  • formatted_phone_number: formatted phone number of the spot (eg (555)555–555)
  • formatted_address: the full address of the spot formatted with commas
  • address_components: the components (eg street address, city, state) of the spot’s address in an array
  • rating: the rating of this spot on Google Places
  • url: the url of this spot on Google Places

Retrieving a list of spots

Retrieve spot with latittude and longitude:

@client.spots(-33.8670522, 151.1957362)

Retrieve spot with specific type:

@client.spots(-33.8670522, 151.1957362, :types => 'restaurant')

Retrieve spot with multiple types:

@client.spots(-33.8670522, 151.1957362, :types => ['restaurant','food'])

Search by multiple types but exclude multiple types:

@client.spots(-33.8670522, 151.1957362, :types => ['restaurant','food'], :exclude => ['cafe', 'establishment'])

Search by name:

@client.spots(-33.8670522, 151.1957362, :name => 'italian')

Search by name and type:

@client.spots(-33.8670522, 151.1957362, :name => 'italian', :types => 'restaurant')

Search in a radius (in meters):

@client.spots(-33.8670522, 151.1957362, :radius => 100)

Get results in specific language:

@client.spots(-33.8670522, 151.1957362, :language => 'en')

Retrieving spots based on query

@client.spots_by_query('Pizza near Miami Florida')

Search by multiple types and exclude multiple types

@client.spots_by_query('Pizza near Miami Florida', :types => ['restaurant', 'food'], :exclude => ['cafe', 'establishment'])

Retrieving a single spot

First register a new Client:

@client = GooglePlaces::Client.new(API_KEY)

Then retrieve the spot:

@client.spot('CmRYAAA...upoTH3g')

Retrieving a single photo’s url

@spot = @client.spot('CmRYAAA...upoTH3g')

Then request one of the photos url with a max width:

url = @spot.photos[0].fetch_url(800)

Search for spots at provide location (with radius)

@client.spot(-33.8670522, 151.1957362, { radius: 10, rankby: "distance", keyword: "Mexican Food" })

* Radius distance is in meters

Radar search

Radar Search Service allows you to search for up to 200 Places at once, but with less detail than is typically returned from a Text Search or Nearby Search request. The search response will include up to 200 Places, identified only by their geographic coordinates and reference. You can send a Place Details request for more information about any of them.

@client.spots_by_radar(-33.8670522, 151.1957362, { radius: 10, rankby: "distance", keyword: "Mexican Food" })

You can access the google places API for more details: https://github.com/qpowell/google_places

I highly reading client.rb which shows all the methods you can use: https://github.com/qpowell/google_places/blob/master/lib/google_places/client.rb

Good luck on your startup! Happy Coding!

Originally published at ademartutor.com on August 3, 2016.

--

--

Ademar Tutor
Ruby on Rails Development

Building AI apps with Ruby on Rails + (Turbo + Hotwire) | React, Langchain, OpenAI