【问题标题】:How to read values from ActiveRecord如何从 ActiveRecord 中读取值
【发布时间】:2016-03-19 08:32:22
【问题描述】:

我正在使用 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 变量中读取latlng 变量?

【问题讨论】:

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


    【解决方案1】:

    正如 Andrey 所说,它是一个哈希数组,您需要选择具有 latlng 变量的数组,这似乎是“邮政编码”哈希。搜索存在“几何”键的哈希。

    所以,

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

    【讨论】:

    • 在第一行之后我有错误:NoMethodError: undefined method `[]' for #<:result::google:0x007fc0e25f1450>
    • 啊,对了,数组在data属性中。我已经修改了我的答案,如果效果更好,请告诉我...
    【解决方案2】:

    来自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 了解有关查询的更多信息。

    【讨论】:

    • 当我尝试时不工作我有这个:NoMethodError: undefined method data for #&lt;Array:0x007fc0e2679d28&gt;
    • @Ponciusz 请立即结帐
    • TypeError: no implicit conversion of String into Integer
    • @Ponciusz 你可以尝试现在编辑的,或者results.geometry.location.lng
    • @Ponciusz 嗯。 how come?
    【解决方案3】:
    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

    【讨论】:

      【解决方案4】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        • 2011-01-12
        • 2018-03-15
        • 2020-03-11
        相关资源
        最近更新 更多