Ruby 地理编码 Gem 结果 methods/hash 参考

Ruby Geocoding Gem results methods/hash reference

result = Geocoder.search("white house, Washington DC")

如何引用以下属性?

我试过r.first.data["address_components"],但是我不能引用过去这个,比如我想拉[type="premise"]["long_name"],等等。如果我想要县,我可以使用 r.first.country 并且有效,但是 r.first.premise 无效。

 [#<Geocoder::Result::Google:0x007f9cda81e880
  @cache_hit=nil,
  @data=
   {"address_components"=>
     [{"long_name"=>"The White House",
       "short_name"=>"The White House",
       "types"=>["premise"]},
      {"long_name"=>"1600", "short_name"=>"1600", "types"=>["street_number"]},
      {"long_name"=>"Pennsylvania Avenue Northwest",
       "short_name"=>"Pennsylvania Ave NW",
       "types"=>["route"]},
      {"long_name"=>"Northwest Washington",
       "short_name"=>"Northwest Washington",
       "types"=>["neighborhood", "political"]},
      {"long_name"=>"Washington",
       "short_name"=>"D.C.",
       "types"=>["locality", "political"]},
      {"long_name"=>"District of Columbia",
       "short_name"=>"DC",
       "types"=>["administrative_area_level_1", "political"]},
      {"long_name"=>"United States",
       "short_name"=>"US",
       "types"=>["country", "political"]},
      {"long_name"=>"20500", "short_name"=>"20500", "types"=>["postal_code"]}],
    "formatted_address"=>
     "The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500, USA",
    "geometry"=>
     {"bounds"=>
       {"northeast"=>{"lat"=>38.8979044, "lng"=>-77.0355124},
        "southwest"=>{"lat"=>38.8973144, "lng"=>-77.03795749999999}},
      "location"=>{"lat"=>38.8976094, "lng"=>-77.0367349},
      "location_type"=>"ROOFTOP",
      "viewport"=>
       {"northeast"=>{"lat"=>38.8989583802915, "lng"=>-77.03538596970849},
        "southwest"=>{"lat"=>38.8962604197085, "lng"=>-77.0380839302915}}},
    "partial_match"=>true,
    "place_id"=>"ChIJGVtI4by3t4kRr51d_Qm_x58",
    "types"=>["premise"]}>]

Geocoder::Result::Googleclass 的文档在这里:http://www.rubydoc.info/github/alexreisner/geocoder/master/Geocoder/Result/Google It has lots of useful methods, including address_components_of_type

看来你要的是这个:

res1 = result.first
premises = res1.address_components_of_type(:premise)
puts premises[0]['long_name']

我在我的代码中添加了以下内容,现在运行良好。

module Geocoder::Result
  class Google < Base

    def premise
       if premise = address_components_of_type(:premise).first
         premise['long_name']
       end
    end

    def premise_code
       if premise = address_components_of_type(:premise).first
         premise['short_name']
       end
    end

    def subpremise
       if subpremise = address_components_of_type(:subpremise).first
         subpremise['long_name']
       end
    end
    def subpremise_code
       if subpremise = address_components_of_type(:subpremise).first
         subpremise['short_name']
       end
    end
  end
end

tl;dr:我认为有一套很好的 docs for Geocoder::Results::Google 可以提供您想要的所有答案。

鉴于上面的结果,这应该有效:

r = result.first
r.types
# => ["premise"]
r.country
# => "United States"
r.country_code
# => "US"
r.formatted_address
# => "The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500, USA"