2014-09-22 64 views
0

使用地理編碼器,我已經能夠解析給定的經度/緯度並獲得城市,省份等。雖然我需要的是子地點,我還沒有看到這個變量。我一直在使用Google地圖自動完成功能,並且在很多建議中都有子地點。例如,從安大略省多倫多的Etobicoke地區,我需要抓住Etobicoke。有沒有辦法與Geocoder做到這一點?使用地理編碼器獲取經緯度的鄰近地點或鄰域

@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 

大部分地址不會有sublocality,所以if語句我用了一個以防止在返回零值時失敗。如果您將neighborhood切換爲sublocality,它可能會給您更好的結果;我仍然必須玩弄它。

相關問題