如何从 ActiveRecord 读取值
How to read values from ActiveRecord
我正在使用 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>]
如何从 results
变量读取 lat
和 lng
变量?
来自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 了解有关查询的更多信息。
正如安德烈所说,它是一个散列数组,您需要 select 具有 lat
和 lng
变量的数组,这似乎是 'postal_code'哈希。搜索存在 'geometry' 键的散列。
所以,
location_results = results.data.select{|r| r['geometry']}.first
location_results['geometry']['location']['lat'] # 40.7214499
location_results['geometry']['location']['lng'] # 73.9520707
result = Geocoder.search("McCarren Park, Brooklyn, NY").first
And then simply call it like this,
-> result.latitude and
-> result.longitude
但也一定要检查结果,有时 Geocoder API 的响应需要超过默认指定的超时值和 returns 零。因此,如果您也遇到这种情况,只需简单地增加初始化器中的超时值即可。
Geocoder::Configuration.timeout = 15 # 将 15 更改为您喜欢的任何值。
有关更多参考资料,请查看 https://github.com/alexreisner/geocoder#advanced-geocoding
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
我正在使用 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>]
如何从 results
变量读取 lat
和 lng
变量?
来自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 了解有关查询的更多信息。
正如安德烈所说,它是一个散列数组,您需要 select 具有 lat
和 lng
变量的数组,这似乎是 'postal_code'哈希。搜索存在 'geometry' 键的散列。
所以,
location_results = results.data.select{|r| r['geometry']}.first
location_results['geometry']['location']['lat'] # 40.7214499
location_results['geometry']['location']['lng'] # 73.9520707
result = Geocoder.search("McCarren Park, Brooklyn, NY").first
And then simply call it like this,
-> result.latitude and
-> result.longitude
但也一定要检查结果,有时 Geocoder API 的响应需要超过默认指定的超时值和 returns 零。因此,如果您也遇到这种情况,只需简单地增加初始化器中的超时值即可。
Geocoder::Configuration.timeout = 15 # 将 15 更改为您喜欢的任何值。
有关更多参考资料,请查看 https://github.com/alexreisner/geocoder#advanced-geocoding
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