繁体   English   中英

如何在Ruby中使用Google Place API?

[英]How to use google place API in Ruby?

以下Ruby代码导致“对等连接重置”

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客户端的解决方案-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

这可能有点杀了,但我只是这样做了:

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哈希,您可以使用它进行任何操作!

另外,我一直在对Local Search API进行大量研究-我认为Google地方信息并不是那么好-您可以从SimpleGeo(免费)获得更多结果

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM