2011-08-23 50 views
0

以下Ruby代码结果如何在Ruby中使用google place API?

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) 

“被连接重置”但同样的HTTP请求的浏览器。什么可能导致差异?

自我回答: 刚刚发现使用Ruby HTTP客户端的解决方案 - 赞助

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 
+0

查看geocoder gem:http://www.rubygeocoder.com/。也许,你不需要重新发明轮子。 – DNNX

回答

1

这可能是一点点过来杀掉,但我就是这样做:

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 

这店是一个JSON散列,你可以做任何你想要的!

而且我已经做了相当多的关于本地搜索API的研究 - 谷歌Places是不是在我看来,伟大的 - 你可以从SimpleGeo获得更多的结果(免费)

感谢