2014-09-22 2 views
0

지오 코더를 사용하여 주어진 위도/경도를 구문 분석하고 도시, 지방 등을 구할 수있었습니다.하지만 내가 필요한 것은 하위 지역이지만, 나는 그것에 대한 변수를 보지 못했습니다. Google지도 자동 완성 기능을 사용하고 있으며 많은 제안 사항에 하위 지역이 있습니다. 예를 들어, Etobicoke, Toronto, Ontario에서 Etobicoke를 잡아야합니다. 지오 코더로이를 수행 할 수있는 방법이 있습니까?위도/경도에서 지오 코더로

@location = Geocoder.coordinates(params[:search_address]) 
    result = Geocoder.search(@location).first 
    cookies[:display_location] = "#{need the region here}, #{result.city}, #{result.state_code}" 

답변

1

내가 생각했던 것보다 응답이 간단해서 찾고 있던 위치 유형에 대한 정확한 용어가 명확하지 않았습니다. Geocoder :: Result 모듈에서 neighborhood 메소드를 발견했습니다.

module Geocoder::Result 
    class Google < Base 

    def coordinates 
     ['lat', 'lng'].map{ |i| geometry['location'][i] } 
    end 

    def address(format = :full) 
     formatted_address 
    end 

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

    def city 
     fields = [:locality, :sublocality, 
     :administrative_area_level_3, 
     :administrative_area_level_2] 
     fields.each do |f| 
     if entity = address_components_of_type(f).first 
      return entity['long_name'] 
     end 
     end 
     return nil # no appropriate components found 
    end 

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

    def state_code 
     if state = address_components_of_type(:administrative_area_level_1).first 
     state['short_name'] 
     end 
    end 

    def sub_state 
     if state = address_components_of_type(:administrative_area_level_2).first 
     state['long_name'] 
     end 
    end 

    def sub_state_code 
     if state = address_components_of_type(:administrative_area_level_2).first 
     state['short_name'] 
     end 
    end 

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

    def country_code 
     if country = address_components_of_type(:country).first 
     country['short_name'] 
     end 
    end 

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

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

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

    def street_address 
     [street_number, route].compact.join(' ') 
    end 

    def types 
     @data['types'] 
    end 

    def formatted_address 
     @data['formatted_address'] 
    end 

    def address_components 
     @data['address_components'] 
    end 

    ## 
    # Get address components of a given type. Valid types are defined in 
    # Google's Geocoding API documentation and include (among others): 
    # 
    # :street_number 
    # :locality 
    # :neighborhood 
    # :route 
    # :postal_code 
    # 
    def address_components_of_type(type) 
     address_components.select{ |c| c['types'].include?(type.to_s) } 
    end 

    def geometry 
     @data['geometry'] 
    end 

    def precision 
     geometry['location_type'] if geometry 
    end 
    end 
end 

나는 코딩에 새로 온 사람이 아마 리팩토링 할 수 있지만, 이것은 내가 사용하고 무엇을 :

 @latlng = Geocoder.coordinates(params[:searched_address]) 
     result = Geocoder.search(@latlng).first 

     if result.address_components_of_type(:neighborhood).first.nil? 
      @display_location = "#{result.city}, #{result.state_code}" 
     else 
      neighborhood = result.address_components_of_type(:neighborhood).first['short_name'] 
      @display_location = "#{neighborhood}, #{result.city}, #{result.state_code}" 
     end 

대부분의 주소는 하위 지역이 없습니다, 그래서 if 문 나는 사용 nil 값을 반환 할 때 실패하지 않도록합니다. neighborhoodsublocality으로 전환하면 더 나은 결과를 얻을 수 있습니다. 나는 아직도 그걸로 놀아야한다.