如何从ActiveRecord

时间:2015-12-13 22:37:58

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord rails-console rails-geocoder

我使用的是https://github.com/alexreisner/geocoder Gem,当我打电话时:results = Geocoder.search("McCarren Park, Brooklyn, NY")我的结果看起来像这里:

[#<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>]

如何从lat变量中读取lngresults变量?

4 个答案:

答案 0 :(得分:1)

来自source code

# Takes a search string (eg: "Mississippi Coast Coliseumf, Biloxi, MS",
# "205.128.54.202") for geocoding, or coordinates (latitude, longitude)
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s.

所以这是一个简单的数组。

结帐this documentation了解有关查询的更多信息。

答案 1 :(得分:1)

正如Andrey所说,它是一个哈希数组,你需要选择具有latlng变量的数组,这些变量似乎是'postal_code'哈希。搜索存在“几何”键的哈希值。

所以,

location_results = results.data.select{|r| r['geometry']}.first
location_results['geometry']['location']['lat'] # 40.7214499
location_results['geometry']['location']['lng'] # 73.9520707

答案 2 :(得分:0)

result = Geocoder.search("McCarren Park, Brooklyn, NY").first

And then simply call it like this, 
-> result.latitude and  
-> result.longitude

但也要确保检查结果,有时Geocoder API的响应需要超过默认指定的超时值并返回nil。因此,如果您也面对这种情况,只需增加初始化器中的超时值即可。

Geocoder :: Configuration.timeout = 15#将15更改为您喜欢的任何值。

有关更多参考资料,请查看https://github.com/alexreisner/geocoder#advanced-geocoding

答案 3 :(得分:0)

location_results = results.data.select{|r| r['geometry']}.first

我现在

["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}}}]

但在此之后

location_results['geometry']['location']['lat']

我再次提出错误:

TypeError: no implicit conversion of String into Integer