我用我用https://github.com/alexreisner/geocoder寶石和讀取數值時,我打電話:results = Geocoder.search("McCarren Park, Brooklyn, NY")
我的結果看起來變種就像這裏:如何從ActiveRecord的
[#<Geocoder::Result::Google:0x007fc0e6a016e0 @data={"address_components"=>
[{"long_name"=>"McCarren Park", "short_name"=>"McCarren Park", "types"=>["point_of_interest", "establishment"]}, {"long_name"=>"776", "short_name"=>"776", "types"=>["street_number"]}, {"long_name"=>"Lorimer Street", "short_name"=>"Lorimer St", "types"=>["route"]}, {"long_name"=>"Williamsburg", "short_name"=>"Williamsburg", "types"=>["neighborhood", "political"]}, {"long_name"=>"Brooklyn", "short_name"=>"Brooklyn", "types"=>["sublocality_level_1", "sublocality", "political"]}, {"long_name"=>"Brooklyn", "short_name"=>"Brooklyn", "types"=>["locality", "political"]}, {"long_name"=>"Kings County", "short_name"=>"Kings County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"New York", "short_name"=>"NY", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}, {"long_name"=>"11222", "short_name"=>"11222", "types"=>["postal_code"]}], "formatted_address"=>"McCarren Park, 776 Lorimer St, Brooklyn, NY 11222, USA", "geometry"=>{"location"=>{"lat"=>40.7214499, "lng"=>-73.9520707}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>40.72279888029149, "lng"=>-73.95072171970848}, "southwest"=>{"lat"=>40.7201009197085, "lng"=>-73.9534196802915}}}, "place_id"=>"ChIJtzAltURZwokRSNfNakChZPU", "types"=>["park", "point_of_interest", "establishment"]}, @cache_hit=nil>]
如何我可以從results
可變的讀取lat
和lng
瓦爾?
第一行後我有錯誤:** NoMethodError:未定義的方法'[]'爲# ** –
Ponciusz
啊,對,該數組是在'data'屬性。我修改了我的答案,讓我知道如果這樣做效果更好... – SteveTurczyn