So i am trying to re-organise some data from the Google Places API, currently it comes out of their API like this:
{"results"=> [
{"geometry"=>{"location"=>{"lat"=>51.503815, "lng"=>-0.11007}}, "icon"=>"http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", "id"=>"5f212d158f4181db3ac0619fb3c52f36d4e276c2", "name"=>"Madeira Cafe", "reference"=>"CnRjAAAApaZWmTl5wOMtW49q3D1BLKAJ_M8lmZxaD6_-AU92qWfVZdokfTWOzlp5En_r9hSUHx-EeP71hzH7iDPYAGPtiqEAXvT4WcI3xlc5XUivenbQLw0j5MHW-ErL-Hbk4xB_by0OSsXCz9etNgkjbp0QCRIQ82Dgj-I3DAJqr7I3EwsFEhoUm2RXf2rCFlSuhfKjSsPuWKA2VGA", {"results"=> [{"geometry"=>{"location"=>{"lat"=>51.503815, "lng"=>-0.11007}},
"icon"=>"http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id"=>"11111111",
"name"=>"Madeira Cafe",
"reference"=>"xxxxx",
"types"=>["restaurant", "food", "establishment"],
"vicinity"=>"London"}]}
The results get put into a hash with one key value – "results"
The rest of the data is the nested inside (i think) with "geometry" being the first of each record.
What I am trying to get to is a neat Hash that has one ID per place, with Lat / Lng and Name stored… so it can be stored and queried.
I have tried something like this:
results_hash = {}
result.each do |geometry, location, id|
results_hash[id] = geometry
end
p results_hash
but I can’t get it to work… it always outputs nil or just the same hash?
I hope this makes sense, as usual if someone just says “read this” its still a great help.
Thanks!
Charlie
Sounds like you want this:
This gives you the following hash (printed with
awesome_print):You’d probably want to format it slightly nicer:
Or if you are using 1.9: