2015-06-21 51 views
1

我试图从Geocoder方法得到州/县。红宝石地理编码器访问结果信息

所以现在我在做result = Geocoder.search('53593')。返回此:

Geocoder: HTTP request being made for http://maps.googleapis.com/maps/api/geocode/json?address=53593&language=en&sensor=false 
=> [#<Geocoder::Result::Google:0x000001092deab0 @data={"address_components"=>[{"long_name"=>"53593", "short_name"=>"53593", "types"=>["postal_code"]}, {"long_name"=>"Verona", "short_name"=>"Verona", "types"=>["locality", "political"]}, {"long_name"=>"Dane County", "short_name"=>"Dane County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"Wisconsin", "short_name"=>"WI", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}], "formatted_address"=>"Verona, WI 53593, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>43.083595, "lng"=>-89.4592069}, "southwest"=>{"lat"=>42.901066, "lng"=>-89.676706}}, "location"=>{"lat"=>42.999243, "lng"=>-89.5686271}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>43.083595, "lng"=>-89.4592069}, "southwest"=>{"lat"=>42.901066, "lng"=>-89.676706}}}, "place_id"=>"ChIJDVeAReqQB4gR9abCGbsr3ls", "postcode_localities"=>["Fitchburg", "Verona"], "types"=>["postal_code"]}, @cache_hit=nil>] 
2.1.1 :016 > 

每文档: 每Geocoder::Result对象,result,提供了以下数据:

result.latitude - float 
result.longitude - float 
result.coordinates - array of the above two 
result.address - string 
result.city - string 
result.state - string 
result.state_code - string 
result.postal_code - string 
result.country - string 
result.country_code - string 

但是,当我尝试做result.stateresult.city我得到一个undefined method 'city'错误。

任何想法我需要做什么来获取该数据?

回答

4

Geocoder回报结果的数组

require 'geocoder' 
result = Geocoder.search '53593' 
result.class 
#⇒ Array < Object 

所以,在这里你去:

result.first.city 
#⇒ "Verona" 
+0

应该想到,一出自己。谢谢! –