Google Place APIでレーダー検索のリクエストを使うと周辺情報が200件取得できるのですが、レーダー検索リクエストの場合詳細情報が取得できないため、詳細検索リクエストを組み合わせてレーダー検索で得たplace_idを元に詳細検索し、緯度と経度と施設名を表示させたいです。(最終的には出力結果をjsonファイルに格納したいです)
しかし25行目で
in `[]‘: no implicit conversion of String into Integer (TypeError) from googleplaceapi.rb:25:in `<main>‘
とエラーが出ます。
配列の扱い方の問題だと思うのですがどのように書き換えればいいか教えていただきたいです。
なおこちらを参考にしています。
https://developers.google.com/places/web-service/search?hl=ja
ruby
1require 'net/https' 2require 'uri' 3require 'json' 4 5API_KEY = 'Your API KEY' 6lat = '-33.8670522' 7lng = '151.1957362' 8rad = '5000' 9 10#types = 'restaurant' 11# types = 'university' 12types = 'hospital' 13#types = 'school' 14 15language = 'en' 16 17uri = URI.parse "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=#{lat},#{lng}&radius=#{rad}&types=#{types}&language=en&key=#{API_KEY}" 18 19request = Net::HTTP::Get.new(uri.request_uri) 20response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| 21 http.request(request) 22end 23 24body = JSON.parse response.body 25results = body['results'] 26 27results.each_with_index do |result| 28 place_id = result['place_id'] 29 30 uri2 = URI.parse "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{place_id}&language=en&key=#{API_KEY}" 31 32 request = Net::HTTP::Get.new(uri2.request_uri) 33 response = Net::HTTP.start(uri2.host, uri2.port, use_ssl: uri2.scheme == 'https') do |http| 34 http.request(request) 35 end 36 37 detail = JSON.parse response.body 38 place_details = detail['results'] 39 40 place_details.each_with_index do |place_detail| 41 location = place_datail['geometry']['location'] 42 lat, lng = location['lat'], location['lng'] 43 44 p "#{location['lat']}, #{location['lng']}, #{place_datail['name']}" 45 end 46end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/08 08:19
2017/09/08 08:34