8 Replies - 1194 Views - Last Post: 15 March 2013 - 10:23 AM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 01:26 AM

So just like you see on dating sites where you select where you are from by State first and then your City that's what I want on my website (it's not dating).

Where is there an API for this? I'm assuming this would have to be a javascript AJAX api or sorts?

Thanks for any tips you may have guys!!
Is This A Good Question/Topic? 0
  • +

Replies To: Where Can I Get An API For A List Of States And Cities?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,382
  • Joined: 12-December 12

Re: Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 03:57 AM

At the moment, from your very brief description, I would suggest just obtaining a list of cities and states. Otherwise, what do you want to do with the information that requires an API?
Was This Post Helpful? 0
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 12:11 PM

View Postandrewsw, on 13 March 2013 - 03:57 AM, said:

At the moment, from your very brief description, I would suggest just obtaining a list of cities and states. Otherwise, what do you want to do with the information that requires an API?



Allowing users to select a city/state where someone is from which will go into my MYSQL database what else friend? I haven't found and API for this which just surprises me to no end.
Was This Post Helpful? 0
  • +
  • -

#4 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Re: Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 12:14 PM

Like Andrew said, you don't really need an API, just a list of states.

If you want to, you can add a list of cities and associate them with states to create a subsequent list of cities after the user has given you the state. To do this, you'd have to add them to a database (like MySQL) and fetch all the cities associated with the chosen state. This could then be done smoothly with AJAX.

There might actually exist an API for that last idea, but I don't know of any :)/>
Good Luck!

EDIT: It appears that Google has such a list/API

This post has been edited by Cbeppe: 13 March 2013 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 01:19 PM

Google is likely definitely the better option here, but just to add an option, the SBA has something of the sort also, however it gives a bit more info about each of the cities.

http://www.sba.gov/a...a-services/7617
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,382
  • Joined: 12-December 12

Re: Where Can I Get An API For A List Of States And Cities?

Posted 13 March 2013 - 02:01 PM

Although there are APIs for this I, personally, would just obtain a list of cities and states and store, and retrieve them, from my database; there aren't that many of them, they don't change often/if at all, and, unless there is other information about the cities that you may want to retrieve, it avoids an unnecessary external request. JMO.
Was This Post Helpful? 3
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Where Can I Get An API For A List Of States And Cities?

Posted 14 March 2013 - 02:53 AM

I Should just use the top like 10 or 20 major cities. If I even did the top 20 that would be 1,000 cities or something but you're right that really isn't that big of a deal.

This post has been edited by Dormilich: 14 March 2013 - 03:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Re: Where Can I Get An API For A List Of States And Cities?

Posted 15 March 2013 - 08:33 AM

If you don't want incredibly long lists to choose from, I'd suggest you get a list of states and a list of counties or cities in the US. Counties if you just need a rough idea where the person is (like for a dating site).

To implement this, you would have a form with two <select> elements where the first asks the user for his state. The second remains hidden or disabled depending on your UI preference.

Now in your DB, you would create two different tables. The first one would have a list of states and the second one a list of cities. The example below shows a basic setup. You can write a small script to get from Google's list of cities in the .csv format and into your MySQL DB.

State Table
+----------+------------+----------------+
| State ID | State Name | State Initials |
+----------+------------+----------------+
|     1    |  Alabama   |       AL       |
|     2    |  Alaska    |       AK       |
|    ...   |  ...       |       ...      |
+----------------------------------------+



City Table
+----------+----------+-------------+
|  City ID | State ID |  City Name  |
+----------+----------+-------------+
|     1    |     1    |  Abbeville  |
|     2    |     1    |  Adamsville |
|    ...   |    ...   |  ...        |
+-----------------------------------+



Once the user has selected his state from the first list, you load the relevant cities from your database using an AJAX query and a query from your database looking like this:

SELECT City Name FROM cities WHERE State ID = $selectedState



You may have to use a join if you're trying to go directly from a state name to a city, but deal with that if it comes. I'm just showing you the basics of how you could make it work. You don't need an API for this but like Andrewsw said above, this is the better option because it saves you an external request.

If you need more help, I'm happy to assist when I'm around :)
Was This Post Helpful? 2
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,382
  • Joined: 12-December 12

Re: Where Can I Get An API For A List Of States And Cities?

Posted 15 March 2013 - 10:23 AM

I was just checking and there are apparently as many as 25,000 US cities (depending on your definition of city). So I would follow Cheppe's advice: populate the first drop-down with a full list of the states, then use an Ajax request to populate, and enable, a second drop-down, based on the state that the user selected.

If it is a dating site I don't think most people would be willing to travel to every state. Well, I suppose it depends on how keen (desperate?) they are :)

This post has been edited by andrewsw: 15 March 2013 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1