ruby - Rails 4 - Parsing and iterating through Google Places API JSON response -
using rails 4 http gem make simple requests. hitting googles api url , couple of parameters in order return nearby stadiums in json.
my goal here iterate through of different stadium results , pull out photo_reference.
i able pull out photo_reference first array so:
def nearby_places(lat, long) response = http.get("https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=mykey&location=#{lat},#{long}&radius=3000&types=stadium") parsed_response = json.parse(response.body) parsed_response["results"][0]["photos"][0]["photo_reference"] end
but having trouble iterating through , pulling out photo references if multiple stadiums returned. best way go this?
here example response google.
{ "html_attributions" : [], "results" : [ { "geometry" : { "location" : { "lat" : 32.2285419, "lng" : -110.9488008 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "09b3cd5bd149c550db4740a67671b1d33b008531", "name" : "arizona stadium", "photos" : [ { "height" : 1371, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/108405732138529965447/photos\"\u003egoogle one\u003c/a\u003e" ], "photo_reference" : "cmrdaaaalckhlhgngofnexfwmbdfw4vg_j5iwbomkoch-hgf8ggtmiymuquvsw7hspfojbcq4is6juhf3lwtqznhyf4b6gtq06ik9mkmcrgjdqgn70vritbhhbgphz77wk8yvcptehb9d3w_kn1w214cqoirswzsghslmg2x44k3cqoibguu-5a_kngkbg", "width" : 2054 } ], "place_id" : "chijs_gb76px1oyrpayw8gequc4", "price_level" : 1, "rating" : 3.8, "reference" : "cnriaaaacucy_bnrsz4ik9mi7w53jtivhxvavvl7ldl1vokgcq3uywopor6oyp1crnks7llacjv1nc_hqjmeu7aiom1h-9ciunc5xl6m79whz7mb7n1niq4gd1hkcxodtqobx_qxx3czlkryg31wsvtumbnu3hiqzzqk_tsjvmmefqsunzl3whouz5o8ezbhcewdhxu7_pig0bx_w4w", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "1 national championship drive, tucson" }, { "geometry" : { "location" : { "lat" : 32.2301948, "lng" : -110.9460055 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "33aa72ea7a9688ca20333c774ecbbc0f4b29eedc", "name" : "mckale memorial center", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "photos" : [ { "height" : 2132, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/102117061543892797349/photos\"\u003ejohn saunders\u003c/a\u003e" ], "photo_reference" : "cmrdaaaacm0q5qsrvvptq77wwhfth_hkityu1bd8jmdjwwdhnqqgb-iaexfc67qjtju0xcdz8mnxblwcfa0lwfotjt8k051_vsmt-cbs00na40rl-mgle10rms4sqbxcoazkyoqzehcxc9ptcqvp_dtqdvr0hju_ghtbwyvv14iram7osdnykb2r4cebeq", "width" : 3097 } ], "place_id" : "chijdwasg6tx1oyrjfkokcr3jwy", "price_level" : 1, "rating" : 4, "reference" : "cnrpaaaaeabkin4gkqua3i2ic0vs4c8k7erbkdtyb6z3eiex4u4w3umbqsgaoeyseotvng2oit1pwa1ksipwqlsyafvjdhas800feebno5w0me-tby7qstmomfo_jpeq4iltcrds3enatmgm9uqnoepe8ntjtriq-lw3poadsj1r4v5rs5uxnbouf8ohthxnvacsf6v3nxefsnblq_s", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "1 national championship drive, tucson" }, { "geometry" : { "location" : { "lat" : 32.2135012, "lng" : -110.9189719 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "d387dca1054f23d5a8767f03f9bb9f7b04f6363c", "name" : "hi corbett field", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "photos" : [ { "height" : 2048, "html_attributions" : [ "\u003ca href=\"https://maps.google.com/maps/contrib/104635440524137932181/photos\"\u003ejose olmos\u003c/a\u003e" ], "photo_reference" : "cmrdaaaav1gofnpoqg-xqmmwhyift-p8go-4hxjwtascavysuresczbodpz5x5dsmsac2zlxnvte_6jzwxky4og0scy-qcees5t6xiqafkcjf8ceqm4vlmkvdj2gxi0mbxdksqwgehaocxh8bddgx41ytsh6i6u8ghqpdxix-s9yvbchyzttacabhphbbg", "width" : 1536 } ], "place_id" : "chijb3zrtjxw1oyrxg2tev6xwa8", "reference" : "cnrjaaaag-sldnko_p9oi52manlzbba2dcn874cswe-hxxl5dejou1hvigcqxbowqxmvr44guwgahima8xkdearpggmgbl8ecdwb71u85pjrm8-j-juuokkq_sxqfjcnf7knbem7bb4vl2soz5vx5wi30udrubiqocz6npauc6ccgj7hi5vcfbou7afjxlr2tdctbos1_3awwppqhey", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "700 south randolph way, tucson" }, { "geometry" : { "location" : { "lat" : 32.2332841, "lng" : -110.9488008 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "a911fe55eaff462d3908c8fea14686f6591660d6", "name" : "arizona wildcats", "place_id" : "chij____k6lx1oyr5zkm9y--p6y", "reference" : "cnrkaaaa7_fi19qo-sax4_ksm1jrdggao9nvnucqo9ccyl8jtwvkt3k_0i0j25qc9ahgkxmhrnpucnhhtsda_wcqrsdbfudmabammx4jxe8wubw-35rxcoodhpgqftt-wlglyugn1jdjp9-awe03b1hhhbwhwbiqyqildlkyktikdhrqfuvi7houh0w6evyplyif1gcpy9zufhud5r0", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "tucson" }, { "geometry" : { "location" : { "lat" : 32.2291294, "lng" : -110.9470374 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "28b19ac3c12af2a0de83694b851a1627c6458ad6", "name" : "mc kaltr", "place_id" : "chijy7kog6tx1oyr5la-ctsxnba", "reference" : "cmrcaaaaz3bqbymatgcg3poewz0t_7blrogdxn6oe-e1uyevy6m_lxk7clsc0mwtsi7m5t-fnuqlrmd-h_kyimy_cayyzrxki5ix580vu3s5ttbuctdjbwtjg1v8dy2hipclipu4ehch52vdsrwqlibzscqilibbghripmyv__gucbpykp1ja8ujkvurgg", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "1 national championship drive, tucson" }, { "geometry" : { "location" : { "lat" : 32.21510419999999, "lng" : -110.9384699 } }, "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "626eb52ef7968c58f0c8c4c9aca05bff1fc38962", "name" : "drachman stadium", "place_id" : "chijvc__01pw1oyrv9wdmsg4pw8", "reference" : "cnrjaaaaln4cwtucpff0kkjo8njl4k57esovzcl5z4kjh6gf4uoc126lnyy1lo-bnmvnfqjk3opub1na5xvvm-bmmshap2if19jindurucchftp6zw_2o8w0-ucotvj4v0oi026dvzjmcfzu8yselhmtjn5cdriqenzfrkga06wbd_4eojb_ebou-pesnldv-4ummr0sdqj9p30lxnw", "scope" : "google", "types" : [ "stadium", "point_of_interest", "establishment" ], "vicinity" : "the of 85719, 501 south plumer avenue, tucson" } ], "status" : "ok" }
Comments
Post a Comment