2013-01-20 7 views
0

지오 코더 젬을 시험해보고 뷰포트 결과를 얻으려고합니다.지오 코더 결과에 액세스

Ruby에 익숙하지 않으면 결과에 액세스하는 더 좋은 방법이 있습니다. 이 동안

result = Geocoder.search("New York, NY").map(&:geometry) 
north_east_lat = result[0]["viewport"]["northeast"]["lat"] 
north_east_lng = result[0]["viewport"]["northeast"]["lng"] 

는이 더 나은 만들기에 추한 부서지기 쉬운

어떤 조언을 보이는 작동합니까?

답변

0

내가 알기 론, geometry 데이터는 단순한 Hash입니다. 여러 수준에서 [ "key"]로 값에 액세스해야하는 방식이 마음에 들지 않으면 해시를 OpenStruct로 변환 할 수 있습니다.

require 'ostruct' 

result = Geocoder.search("New York, NY").first 
# singleton class http://www.devalot.com/articles/2008/09/ruby-singleton 
class << result 
    def ostructic_geometry 
    ostructly_deep self.geometry 
    end 

    private 
    def ostructly_deep(hash) 
     root = OpenStruct.new 

     # http://www.ruby-doc.org/stdlib-1.9.3/libdoc/ostruct/rdoc/OpenStruct.html#method-i-marshal_load 
     # -- from the user comment at the bottom -- 
     # In the marchal_load() example, the Hash should have symbols as keys: 
     # hash = { :time => Time.now, :title => 'Birthday Party' } 
     load_data = hash.each_pair.inject({}) do |all, (key, value)| 
     value = ostructly_deep(value) if value.is_a?(Hash) 
     all[key.to_sym] = value # keys need to be symbols to load 
     all 
     end 

     root.marshal_load load_data 
     root 
    end 
end 

now_you_can_call_value_from_member_geometry = result.ostructic_geometry 
now_you_can_call_value_from_member_geometry.bounds.northeast.lat # => 40.9152414 
0

보이지 않습니다. geometry은 매우 Google 전용 필드이므로 Google Maps API 결과에만 정의됩니다. 좌표 (지오 코더가 이미 추출한 좌표)뿐만 아니라 떨어지는 것으로 간주되지 않는 location_type, viewportbounds을 포함합니다. 지오 코더의 표준 사용 사례 내에서 : location_type은 결과의 정밀도와 관련이 있습니다. viewport은 시각적지도에 Google이이 결과를 표시하는 방법을 독점적으로 말합니다. bounds은 전체 도시/국가/기타에 대한 경계 상자입니다. . 매우 특정한 유스 케이스에서는 각각 관련성이 있지만 지오 코더를 사용하는 대부분의 사람들은 필요하지 않으므로 개발자는 책임을지지 않고 직접 필드를 노출합니다. 따라서 이러한 필드에 액세스 할 수있는 확실한 방법을 원할 경우 직접 작성해야합니다.

자주 뷰포트 기능을 사용하는 경우, 그 다음, 자신의 Viewport 클래스가이 데이터를 표현하기 위해 만드는 수동 표현을 포장 (Viewport.from_geometry(result.geometry)를 같은) 또는 Geocoder::Result::Google에 자신의 viewport 방법을 패치 가치가있을 수도 있습니다. 너의 전화.

1

사용자는 예를 들어, 그 데이터에 액세스하기 위해 지오 코더 :: 결과 방법을 사용할 수

모든 방법을 참조 .latitud [0]

결과 [0] .city 결과 with, result [0] .methods