How can I find the nearest 3 zip codes locations from the zip code entered?

I am implementing Google Map Zip Code Search and my previous question here
I have 100 zip codes of US and it is all fixed zip codes with address and to be searched this database by the user to enter zip code only  ...
based on this How can I pull the nearest 3 locations from its database ? I mean nearest 3 zip codes to be find ? Please help me.

At present, my code is implemented just search for single zip code and the details fetched from the db and shown on the google map
LVL 36
Loganathan NatarajanLAMP DeveloperAsked:
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
See if this article can give you the guidance you need.

There are about 42,000 ZIP codes in the US.  Most of the free ZIP-to-address data bases are full of inaccuracies, so be careful to scrub your data.  Best regards, ~Ray
Jagadishwor DulalBraces MediaCommented:
One of the good way to show exactly nearest 3 zip is adding nearest zip in database so that you can easily manage it.
I don't know about your table structure but may add nearest1, nearest2, nearest3 and zip code for them. But if you want to get zip code by increment no like 1111, 1112, 1113, 1114 when you select 1111 you may create a select query which can retrieve 3 records from current searched record if searched zip found.
Loganathan NatarajanLAMP DeveloperAuthor Commented:
I have like this my table structure ?
<test_name>This is name details</test_name>
<address>5, street</address>

Open in new window

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Loganathan NatarajanLAMP DeveloperAuthor Commented:
In fact, what I have the zip codes are only California
Jagadishwor DulalBraces MediaCommented:
Then it's better to use where it shows zips
VampireofdarknessConnect With a Mentor Commented:
You will need to either have the latitude and longitude to calculate an 'as the crow flies' distance using the Great Circle calculations (many are available from a quick google search), this will not be precise for drive times though, or to use an API (such as Google Maps) to geocode and calculate the driving distance between the two points then save that to your database in a separate table:

Table: distances
Zip1: xxxxx
Zip2: yyyyy
Distance: zzzzzz

Then you would search for SELECT Zip1,Zip2,Distance FROM distances WHERE Zip1='Zip to search' OR Zip2='Zip to search' ORDER BY Distance ASC Limit 0,3

If you do one-way (x to y and not y to x) you use the above, if you do two-way (calculate x to y and y to x, as it may take different roads) you only need Zip1 in your WHERE clause.

This is the more accurate method and you can cache results, but you will need to set up the database first and Google's API is limited to 2,500 calls in one day for one IP address so depending on the size of your database, it could be infeasible. Google's Premier accounts I think raise this to 100,000 calls in a day.

To get two-way distances (even more accuracy) you'll need more results than one-way. I think it works out to n*(n-1) where n is your number of records (so 10 entries = 90 searches of an API, 8,000 entries = 63,992,000 searches)

Note: If you are using American zip codes, I think there is another service called that also allows a certain number of free queries per day. I can't remember if that has an API though.
Loganathan NatarajanLAMP DeveloperAuthor Commented:
@ Ray_Paseur

Thanks, I will look at it.
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.