rugbert
rugbert

Reputation: 12623

How to pull Google Places info with Rails 3?

So I know that google places API serves the place information (name, location, website, type ect) with JSON but I was wondering how to grab the data from the JSON?

Basically I've got a site where users can use the google maps API Ive set up to search for their retail shops and Id like to store all that information in a table so that way when you go to the user's profile page you can see their google place information on a integrated map.

Maybe theres an easier way to do that but Im not sure how.

Upvotes: 1

Views: 3934

Answers (3)

komaldhanwani
komaldhanwani

Reputation: 101

For using Google Places API with Rails, you can use the Gem google_places:: https://github.com/marceldegraaf/google_places

I hope this will help you.

Upvotes: 1

Lance Pollard
Lance Pollard

Reputation: 79178

You should only use the google places api from the client. Google has a rate limit set pretty low, and it's based on IP address (at least for the google maps api). So if you're making too many requests from your web server, all of a sudden google cuts you off for the day. The way around this is to just do all the calls in javascript, so it's coming from the users IP address.

When the user types their address into your form, use the google places api to get the place id, or lat/lng coordinates, then save those along with everything else in your database. Then just use that data to fetch the place json from the google places api when they view the user profile.

Update

According to the google places docs, it's actually the "reference token" you want to store, not the id.

Since you want users to be able to search for their stores, you need to make 2 queries:

  1. First, you need to have lat/lng coordinates to be able to query the places api (google places api docs. You do this by querying google maps' geocoding api with, say, the users current location, or some address they filled out when signing up. The response will return, among other attributes, the lat/lng coordinates. Save those to your database tied to the user model. I've done this with Geocoder, but because of google's rate limiting, you quickly run into problems. Keep all the geocoding stuff client side, then save the results with ajax or whatever.
  2. Then, on the page where the user searches for their retail stores, you just query the places api with their lat/lng coordinates: google places api search request example. You'll get a list of results. For each place the user selects, you save the reference token.
  3. Then when you want to retrieve the details for that place, you make a google places api 'place details' request using the reference token.

Upvotes: 7

Benjamin
Benjamin

Reputation: 2118

I thought of doing the same with Geocoder, i think it's worth looking at.http://railscasts.com/episodes/273-geocoder?view=comments

Upvotes: 1

Related Questions