1

I just installed Ruby Geocoder and I am trying to traverse the Geocoder::Result, but keep coming up short. I am in rails console, and have used the following:

s = Geocoder.search("90210")

Which returns the following:

[#<Geocoder::Result::Google:0x007ff295579ad8 @data={"address_components"=>[{"long_name"=>"90210", "short_name"=>"90210", "types"=>["postal_code"]}, {"long_name"=>"Beverly Hills", "short_name"=>"Beverly Hills", "types"=>["locality", "political"]}, {"long_name"=>"Los Angeles", "short_name"=>"Los Angeles", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"California", "short_name"=>"CA", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}], "formatted_address"=>"Beverly Hills, CA 90210, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>34.1354771, "lng"=>-118.3867129}, "southwest"=>{"lat"=>34.065094, "lng"=>-118.4423781}}, "location"=>{"lat"=>34.1030032, "lng"=>-118.4104684}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>34.1354771, "lng"=>-118.3867129}, "southwest"=>{"lat"=>34.065094, "lng"=>-118.4423781}}}, "types"=>["postal_code"]}, @cache_hit=nil>]

How can I access a specific part of the Result? I have tried the following:

s.result.city, result.city, s.data?? But none of these work. Any ideas would be great.

Thanks!

4

3 回答 3

3

我想到了。文档说“Geocoder.search 返回一个 Geocoder::Result 对象数组”

因此,我需要这个:

s[0].city<= 比佛利山庄或 s.first.city(感谢@fotanus)

于 2013-09-07T02:23:21.427 回答
1

According to the docs, it is a struct, so you should access the values as an indexed symbol, like the following:

result[:city]

The link has what can be accessed from this object.

于 2013-09-07T02:12:23.707 回答
0

使用“Geocoder.search”时,需要经过几层解析才能到达城市。以下将返回“比佛利山庄”。

s.first.data["address_components"][1]["short_name"]

如果您想使用其他答案中看到的语法,则需要创建一个新的地理编码器实例,而不是使用 Geocoder.search

于 2013-09-19T22:15:34.087 回答