如何根据用户输入的邮政编码输入城市/州/国家/地区代码? [英] How to do a city/state/country code lookup based on zip/country input by the user?

查看:135
本文介绍了如何根据用户输入的邮政编码输入城市/州/国家/地区代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以根据用户输入的邮政编码来查找城市/州/国家/地区代码?我的网站是国际化的,因此要求用户输入他们的国家/地区的原因.

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.

我在考虑用户输入的邮政编码和国家/地区,然后将其保存到数据库中,然后Google Geocode API会将其转换为城市,州和国家/地区,并将输出打印到他们的用户个人资料中.例如:

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:

用户输入:

Zip - 92646

Country - USA

输出:

Huntington Beach, CA, USA

我可以让用户输入他们的城市,州和国家/地区,但将来我想进行一些地理编码.因此,现在就进行设置而不是在稍后阶段迁移数据库是有意义的.还是您认为我在这里做错了?我有一个内置于Rails的网站.预先感谢.

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.

**评论:看起来像 RubyCoder 上的演示一样,Gem允许您输入邮政编码和国家(地区)打印城市/州/国家/邮政编码,这正是我所追求的.关于Geocoder和Google Goecoder API的想法?

** 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?

推荐答案

我建议使用地理编码器gem,并允许用户在一个字段中输入其地址.用户更容易以一种方便的格式输入一个字段.并将其保留为完整地址.然后将此地址提供给地址解析器(通常,地址解析器会自动执行此操作),然后从该位置获取坐标,城市,州等. 如果用户输入了错误的地址,则只需对其进行澄清即可.这只是我的看法,而不是规则.

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.

这篇关于如何根据用户输入的邮政编码输入城市/州/国家/地区代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆