我试图通过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.state
或result.city
时,我收到undefined method 'city'
错误。
知道我需要做些什么才能获得这些数据?
答案 0 :(得分:4)
Geocoder
会返回数组结果:
require 'geocoder'
result = Geocoder.search '53593'
result.class
#⇒ Array < Object
所以,你走了:
result.first.city
#⇒ "Verona"