0

Would there be a way to do a city/state/country code lookup based on zip/country input by the user? My site will be international, hence the reason for asking the user to input their country.

I'm thinking the user inputs the zip/post-code and country, which gets saved to the database and then the Google geocode API will convert this to city, state and country and print the output to their user profile. For example:

User input:

Zip - 92646

Country - USA

Output:

Huntington Beach, CA, USA

I could just let the users input their city, state and country, but in the future I want to do some geocoding. So it makes sense to set it up now rather than migrate the database at a later stage. Or do you think I'm doing the wrong thing here? I have a site built in Rails. Thanks in advance.

** Comment: Looks like the demonstration on the RubyCoder Gem allows you to input the zip and country to print the City/State/Country/Zip, which is exactly what I'm after. Thoughts on Geocoder versus Google goecoder API?

glennm
  • 325
  • 1
  • 5
  • 12
  • 2
    Depends what you're comfortable with. I write straight JS for the Google Maps API, you'll save a lot of typing with the Ruby Geocoder. The JS looks like this: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingStatusCodes The ruby geocoder supports Google as a data source. – Heitor Chang Apr 30 '12 at 16:57
  • as said by @HeitorChang, geocoder uses sources like google, so why would you want to interact directly with the API? – Ismael Abreu Apr 30 '12 at 17:01
  • I'm very new to rails, so maybe the Geocoder would be 'easier' for me. – glennm Apr 30 '12 at 17:06
  • If you use the Google geocoder, you **must** show the location on a map to comply with the Terms of Use. This may not be an issue (pictures are good and you can show the user where you have located them) but I just thought I'd mention it. – Andrew Leach May 01 '12 at 06:53
  • Thanks @AndrewLeach. I don't mind showing the map if it's part of the T&C's. Like you said, pictures are good! – glennm May 01 '12 at 15:34

1 Answers1

0

I would advise to use the geocoder gem and allow the user to enter their address on one field. It is easier for the user to enter only one field in it a convenient format. And keep it string as a full address. Then give this address to geocoder (in general geocoder will do it automatically), and from there take the coordinates, city, state, etc. If the user enters a bad address, he simply clarify it. This is just my opinion, not the rule.

Mik
  • 4,117
  • 1
  • 25
  • 32
  • Interesting. I was originally thinking that I should have two inputs (zip and country) in the database. I'd much prefer to have one input for a better user experience. After lookign at the Railscast Geocoder I see that the Google API corrects spelling, which was my original concern for having one line input. – glennm Apr 30 '12 at 17:29
  • The only problem may be if the user incorrectly entered his address. For example, he entered only a street name and a street with the same name already has in another city (or another country). In this case, Geocoder might assign him to another city. I think the solution is to show the user data after geocoding and ask: "Is this correct?". This can be done even through Ajax. – Mik Apr 30 '12 at 17:46
  • I'm assuming I only need to add one column to the database (Address) and don't need one for the latitude and longitude? Will Geocode be able to convert the address without my database firstly converting to a lat and long and pasting in the database? – glennm Apr 30 '12 at 21:46