Search code examples
ruby-on-railsrubymethodsgeocodingrails-geocoder

Finding Boroughs with Geocoder


I'm using the Geocoder gem for my rails project and there's one small problem I seem to be running into. I have a user model that has an address attribute. This attribute stores information as follows:

geocoded_by :address do |obj,results|
  if geo = results.first
    obj.state = geo.state
    obj.country = geo.country
    obj.city = geo.city
  end
end

This works just fine but if I try to enter an address with a borough, geocode only returns the city. So for example if I try to save Brooklyn, New York it will return New York, New York but if I enter a town such as Middletown, Delaware it has no problem displaying its full location. Is there a parameter I can use to query boroughs? I've tried geo.borough but got errors.


Solution

  • There aren't very many borough's in the US (see here).

    As you probably know, Brooklyn, New York is in New York, New York so the fact that the city data responds with New York is 100% accurate.

    That said, I do see The Bronx in an sample call I made to the google maps api here:

    https://maps.googleapis.com/maps/api/geocode/json?address=610+Waring+Ave,+Bronx+NY

    [2] pry(main)> HTTParty.get('https://maps.googleapis.com/maps/api/geocode/json?address=610+Waring+Ave,+Bronx+NY')
    
    => {"results"=>
      [{"address_components"=>
         [{"long_name"=>"610", "short_name"=>"610", "types"=>["street_number"]},
          {"long_name"=>"Waring Avenue", "short_name"=>"Waring Ave", "types"=>["route"]},
          {"long_name"=>"Bronx",
           "short_name"=>"Bronx",
           "types"=>["sublocality_level_1", "sublocality", "political"]},
          {"long_name"=>"Bronx County",
           "short_name"=>"Bronx County",
           "types"=>["administrative_area_level_2", "political"]},
          {"long_name"=>"New York",
           "short_name"=>"NY",
           "types"=>["administrative_area_level_1", "political"]},
          {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]},
          {"long_name"=>"10467", "short_name"=>"10467", "types"=>["postal_code"]}],
        "formatted_address"=>"610 Waring Avenue, Bronx, NY 10467, USA",
        "geometry"=>
         {"location"=>{"lat"=>40.861204, "lng"=>-73.870415},
          "location_type"=>"ROOFTOP",
          "viewport"=>
           {"northeast"=>{"lat"=>40.8625529802915, "lng"=>-73.8690660197085},
            "southwest"=>{"lat"=>40.8598550197085, "lng"=>-73.8717639802915}}},
        "types"=>["street_address"]}],
     "status"=>"OK"}
    

    You can see in this file that they're not currently supporting anything but those params. Maybe a pull request is in order as this feature would be good for me as well.