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

Posted on 2011-05-04
Last Modified: 2012-05-11
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
Question by:Loganathan Natarajan
    LVL 15

    Expert Comment

    by:Jagadishwor Dulal
    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.
    LVL 36

    Author Comment

    by:Loganathan Natarajan
    I have like this my table structure ?
    <test_name>This is name details</test_name>
    <address>5, street</address>

    Open in new window

    LVL 36

    Author Comment

    by:Loganathan Natarajan
    In fact, what I have the zip codes are only California
    LVL 15

    Expert Comment

    by:Jagadishwor Dulal
    Then it's better to use where it shows zips
    LVL 9

    Assisted Solution

    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.
    LVL 107

    Accepted Solution

    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
    LVL 36

    Author Comment

    by:Loganathan Natarajan
    @ Ray_Paseur

    Thanks, I will look at it.
    LVL 36

    Author Closing Comment

    by:Loganathan Natarajan

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now