簡體   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