开发者

How to auto-complete a form field with a city and state?

I have a free form textfield that allows a u开发者_StackOverflowser to type in a city and state, e.g. "Chicago, IL".

What I would like to do is when the user starts typing, to begin displaying suggestions of completed city+state combinmations (much like how Stackoverflow auto-suggestions the "tags" field below when you create a question).

Question: Does an API exist that returns every U.S.A city + state combination? Or can I download every city+state that exists in the USA from somewhere?


Yes, there are a number of solutions available for the same task, you can try out this one. Also you will have to download USA states mysql database and then modify the table names or the script for the autocomplete class to work successfully.

Here is another good-looking option available.

http://www.nodstrum.com/2007/09/19/autocompleter/


Not sure of any existing APIs to do this exact thing for you, but here's some tools to get you started:

  • (Data to use for auto-completion) http://www.census.gov/tiger/tms/gazetteer/zips.txt

Then you could just return the top 5 cities/state combos that match whatever first input the user has entered after each keystroke.


http://geonames.org has an API to search for every city, landmark, mountain, etc. I use it myself. As for auto complete, the other answers are good resources.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜