Hello,

Can anyone offer advice/assistance with calculating the distance between two zip codes? I must implement this with php and mysql.

A free solution would obviously be best 🙂

Thanks,

--SL

    Hello,

    I'm in the same boat, just wondering if you found anything.

    You may be ahead of me in fact -- as far as zip codes, I can get a list of them, but how will these be kept updated over time (if at all)?

    Anyway, I'll keep looking and post if I find something.

    Rick

      You don't just need a list of Zip codes, but also the latitude and longitude associated with those Zips. Once you have those, you can find your distance.

      I had to write a script to find all zips within a 10 mile radius of a given zip. It is possible, but very complex. It gets even worse when dealing with Canadian postal codes. There are approx 45,000 US zip codes, but over 880,000 Canadian postal codes! Mysql doesn't like searching that table too often.

        Thanks for your reply,

        How are the zip/postal/latitude/longitude lists obtained?

        Does anyone have this in database format already?

        Can anyone offer a sample script?

        Thanks again,

        --SL

          I'm sure someone who has done this before will provide better help, but I'm finding out some info...

          I downloaded a list of zips, but **** if I can remember where I got it. I'll see if I can find the link again (got it through google). The file I downloaded was zip.txt.gz. It contains a comma-delimited list of zips, states, cities and longitude/latitude info.

          You'd probably want to import that into your database, I'm more familiar with SQL server and postgresql than mysql, so I can't help out too much with specifics there.

          As far as scripts to do the calculations, I haven't found any free ones yet. There's this one for $74 that does exactly what we want: http://keskydee.com/geozip.php

          I'm probably going to try using code from this page (it's Perl but the algorithm should be the same in any language) unless I find a ready-to-go script:
          http://jan.ucc.nau.edu/~cvm/latlongdist.html

          My biggest concern is keeping the zips up-to-date. We are building the site for someone and turning it over to them, so unless we tack on a maintenance clause, the db will eventually be obsolete to some degree. Hate to tell the client that they need to hire someone every year to grab another db, but if that's what they have to do so be it.

          Anyway, I'll try to put together something from that script and if it works you're welcome to it. I'm pretty slow with php though, I normally use Cold Fusion!

          Rick

            I think...

            I found some code that seems to do the trick once you have longitude and latitude values (which is the easy part, that will be in the zip code database table). Once you have the two zips, pull the lat and lon values for each zip and apply the following code. The lat and lon values are hardcoded in this test. It seems accurate so far in my trials (changing to different lat and lon values), but I haven't done too much testing yet.

            I only need miles, but I left the other stuff in there.

            <?php

            $pi = 3.1415926;
            $rad = doubleval($pi/180.0);

            $lat1 = 28.395724;
            $lon1 = -81.46660;
            $lat2 = 28.467258;
            $lon2 = -81.45248;

            $lat1 = doubleval($lat1)$rad;
            $lon1 = doubleval($lon1)
            $rad;
            $lat2 = doubleval($lat2)$rad;
            $lon2 = doubleval($lon2)
            $rad;

            $theta = $lon2 - $lon1;
            $dist = acos(sin($lat1) sin($lat2) + cos($lat1) cos($lat2) cos($theta));
            if ($dist < 0) { $dist += $pi; }
            $dist = $dist
            6371.2;
            $miles = doubleval($dist 0.621);
            $inches = doubleval($miles
            63360);
            $dist = sprintf("%.2f",$dist);
            $miles = sprintf("%.2f",$miles);
            $inches = sprintf("%.2f",$inches);

            ?>

            <p>dist: <? echo $miles ?>

              Thanks for help,

              I'm going to see if I can locate that zip.txt.gz and go from there. Will try your code and see if everything works out.

              --SL

                Thank you,

                So does the site above.

                Here is an extremely simplified answer to my problem.

                $lat1 = getLat($zipcode);
                $lon1 = getLon($zipcoe);

                $zipquery = "select from zips where (39583.1415926sqrt((lat-'$lat1')(lat-'$lat1') + cos(lat/57.29578)cos('$lat1'/57.29578)(lng-'$lon1')*(lng-'$lon1'))/180) <= '$within'";

                This query caculates results in only zip codes $within a certain distance for the original $zipcode.

                Thanks everyone,

                --SL

                  a month later

                  You search of solution should end here. Visit this URL and the developers section.

                  http://www.zipcodeworld.com

                  They have:

                  a) Distance calculation explanation
                  b) Source codes in ASP/PHP/C/C#/VB etc
                  c) Longitude, Latitude and ZIP Codes

                  ...

                    Write a Reply...