Reputation: 5735
result = Geocoder.search("white house, Washington DC")
How do you reference the below attributes?
I have tried r.first.data["address_components"]
, but I can not reference past this, for example, I want to pull the [type="premise"]["long_name"], etc. If I want the county, I can use r.first.country and it works, but r.first.premise does not work.
[#<Geocoder::Result::Google:0x007f9cda81e880
@cache_hit=nil,
@data=
{"address_components"=>
[{"long_name"=>"The White House",
"short_name"=>"The White House",
"types"=>["premise"]},
{"long_name"=>"1600", "short_name"=>"1600", "types"=>["street_number"]},
{"long_name"=>"Pennsylvania Avenue Northwest",
"short_name"=>"Pennsylvania Ave NW",
"types"=>["route"]},
{"long_name"=>"Northwest Washington",
"short_name"=>"Northwest Washington",
"types"=>["neighborhood", "political"]},
{"long_name"=>"Washington",
"short_name"=>"D.C.",
"types"=>["locality", "political"]},
{"long_name"=>"District of Columbia",
"short_name"=>"DC",
"types"=>["administrative_area_level_1", "political"]},
{"long_name"=>"United States",
"short_name"=>"US",
"types"=>["country", "political"]},
{"long_name"=>"20500", "short_name"=>"20500", "types"=>["postal_code"]}],
"formatted_address"=>
"The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500, USA",
"geometry"=>
{"bounds"=>
{"northeast"=>{"lat"=>38.8979044, "lng"=>-77.0355124},
"southwest"=>{"lat"=>38.8973144, "lng"=>-77.03795749999999}},
"location"=>{"lat"=>38.8976094, "lng"=>-77.0367349},
"location_type"=>"ROOFTOP",
"viewport"=>
{"northeast"=>{"lat"=>38.8989583802915, "lng"=>-77.03538596970849},
"southwest"=>{"lat"=>38.8962604197085, "lng"=>-77.0380839302915}}},
"partial_match"=>true,
"place_id"=>"ChIJGVtI4by3t4kRr51d_Qm_x58",
"types"=>["premise"]}>]
Upvotes: 0
Views: 89
Reputation: 1376
tl;dr: There's a good set of docs for Geocoder::Results::Google available that has all the answers you'll want, I think.
Given your result above, this should work:
r = result.first
r.types
# => ["premise"]
r.country
# => "United States"
r.country_code
# => "US"
r.formatted_address
# => "The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500, USA"
Upvotes: 0
Reputation: 5735
I added the following to my code and now it works well.
module Geocoder::Result
class Google < Base
def premise
if premise = address_components_of_type(:premise).first
premise['long_name']
end
end
def premise_code
if premise = address_components_of_type(:premise).first
premise['short_name']
end
end
def subpremise
if subpremise = address_components_of_type(:subpremise).first
subpremise['long_name']
end
end
def subpremise_code
if subpremise = address_components_of_type(:subpremise).first
subpremise['short_name']
end
end
end
end
Upvotes: 0
Reputation: 106027
The documentation for the Geocoder::Result::Google class is here: http://www.rubydoc.info/github/alexreisner/geocoder/master/Geocoder/Result/Google It has lots of useful methods, including address_components_of_type
.
It looks like what you want is this:
res1 = result.first
premises = res1.address_components_of_type(:premise)
puts premises[0]['long_name']
Upvotes: 1