Chuankai
Chuankai

Reputation: 151

How to use google place API in Ruby?

The following Ruby code results 'connection reset by peer'

request = 'https://maps.googleapis.com/maps/api/place/search/json?location=23.001202,120.191889&radius=50&sensor=false&key=MY_GOOGLE_API_ACCESS_KEY'
puts Net::HTTP.get(URI.parse request)

But the same HTTP request works in a browser. What's could possibly cause the differences?

Self-answering: Just found an solution using Ruby HTTP client -- Patron

require 'patron'
sess = Patron::Session.new
sess.base_url = "https://maps.googleapis.com/"
res = sess.get "maps/api/place/search/json?location=23.001202,120.191889&radius=50&sensor=false&key=GOOGLE_PLACE_API_KEY"
puts res.body

Upvotes: 0

Views: 861

Answers (1)

Charlie Davies
Charlie Davies

Reputation: 1834

This may be a little over kill but I just did this:

require 'rubygems'
require 'open-uri'
require 'json'
require "awesome_print"


baseurl = "https://maps.googleapis.com/maps/api/place/search/json?"
lat = 51.50364209455692.to_s
lng = -0.10880472473149894.to_s
radius = 500.to_s
type = "bar"
sensor = "true"
key = "MYKEY"

combine = baseurl + 'location=' + lat + ',' + lng  + '&' + 'radius=' + radius + '&' + "types=" + type + '&' + "sensor=" + sensor +  '&' + "key=" + key

url = combine
result = open(url) do |file|
  JSON.parse(file.read)
end

This stores is a JSON hash that you can do what ever you want with!

Also I have been doing quite a bit of research on Local Search API - Google Places isn't that great in my opinion - You can get a lot more results (for free) from SimpleGeo

Thanks

Upvotes: 1

Related Questions