1
votes

Using Geocoder, I've been able to parse a given latitude/longitude and get the city, province, etc.. Though what I need is the sub-locality, and I haven't seen a variable for that. I've been using Google Maps Autocomplete, and there are sub-localities in a lot of the suggestions. For example, from the location Etobicoke, Toronto, Ontario, I need to grab Etobicoke. Is there a way to do this with 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

1 Answers

1
votes

Answer was simpler than I thought, I was just unclear on the correct term for the location type I was looking for. Inside the Geocoder::Result module, I found the method 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

I'm new to coding and this could probably be refactored, but this is what I'm using:

      @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

Most addresses will not have a sublocality, so I used an if statement to prevent it failing when returning a nil value. It might give you better results if you switch neighborhood with sublocality; I've still got to play around with it.