I am using geocoder
with Google to validate addresses and give me back the correct values if they are munged slightly as well as to acquire latitude and longitude. I think I may be a bit off in my implementation here because it just feels so clunky and also I'm finding that some of the addresses are missing the street number.
I am starting off with the following code:
result = Geocoder.search(full_address_to_verify)
if result != []
street_address = result[0].address_components[1]["long_name"]
city = result[0].address_components[2]["long_name"]
state = result[0].address_components[5]["long_name"]
zip = result[0].address_components[7]["long_name"]
end
I have found that sometimes I get multiple results, and so I am going with the first one ([0]
above). Further, I have found some queerness in that most of the time result[0].address_components[1]
contains the full address, including the street_number, but once in a while it does not and I have to then add result[0].address_components[0]
. I have yet to figure out a rhyme or reason to this.
Ultimately my goal here is to retrieve the USA street number + street, city, state and zip code fields into separate variables.
So my question is, is there a better way to retrieve the address so that the street number is always associated properly with the route?
From the documentation I'm not really seeing any clean methods that would just simply give me the desired address fields without pulling the data out of the individual fields from the request reply...
A couple of years late to the party. Extracting an address from the google geocoding API is straightforward and rather cumbersome with the nested address_components.
Luckily, the geocoder gem has built-in methods to access the address info. Read more on the docs.