# Thread: Zip Code Search With Distance

1. ## Zip Code Search With Distance

I am currently buidling a mini-application (php and mysql) of store locations that are in the united states. People that will use this search tool will be searching by zip code.

The idea is not to have a "no results found" but rather give a result of the nearest store to the entered zip code.

The way i thought to do this was to use a webservice to calculatte the distances between zip codes.

Can anyone recommend which webservice would be compatible for this or suggest another solution?

2. You need to purchase a database that contains GPS info for each zip code. Then your query needs to do math to find other zipcodes at the specified distance by doing math based longitude and latitude of each zipcode.

3. you can do this yourself.

get a zipcode database, probably \$50 or so for a good up to date one that includes latitude and longitude(i havent checked prices for a while). you might be able to find a free one, although it may not be as accurate or up to date.

then its a matter of math.

in your case i think you would:
grab the users starting zipcode, query db to get lat/long
then query your db to compare and calculate distance for the lat/long of each of your stores. order results by distance.

i know some of the companys that sell these databases also offer a webservice. only problem with this is its going to be slower, and your code will fail if you cannot contact thier webservice due to network issues.

4. I've done something quite similar and it's pretty easy to do. And I'm a complete newbie! You'll need this function...

PHP Code:
``` function distance(\$lat1, \$lon1, \$lat2, \$lon2, \$unit) {    \$theta = \$lon1 - \$lon2;    \$dist = sin(deg2rad(\$lat1)) * sin(deg2rad(\$lat2)) +  cos(deg2rad(\$lat1)) * cos(deg2rad(\$lat2)) * cos(deg2rad(\$theta));    \$dist = acos(\$dist);    \$dist = rad2deg(\$dist);    \$miles = \$dist * 60 * 1.1515;   \$unit = strtoupper(\$unit);   if (\$unit == "K") {     return (\$miles * 1.609344);    } else if (\$unit == "N") {       return (\$miles * 0.8684);     } else {         return \$miles;       } }  ```
The way I did it was to have a CSV file of all the postcodes (British) and their long, lats. Then I wrote some code to check the distance between two postcodes submitted in a form. Not exactly what you are after but it will hopefully be a start. Excuse my code. Like I said, i'm a newbie!

PHP Code:
``` \$pcode1 = \$_GET['pcode1']; \$pcode2 = \$_GET['pcode2']; \$file = fopen('postcodes.csv', 'r'); while ((\$psearch = fgetcsv(\$file , 10000, ",")) !== FALSE) {     if (\$psearch[0] == \$pcode1)     {         \$p1long = \$psearch[4];         \$p1lat = \$psearch[5];     }           if (\$psearch[0] == \$pcode2)     {         \$p2long = \$psearch[4];         \$p2lat = \$psearch[5];     }  } fclose(\$file); echo "Distance from ".\$pcode1." to ".\$pcode2." is "; printf ( "%.2f", distance(\$p1long, \$p1lat, \$p2long, \$p2lat, "M")); echo " miles.<br>";  ```
Of course the hard part will probably be finding the zipcode CSV file.

Hope this helps.

5. You could probably use a thrid-party like Google Maps, however i don't know of all what you can do with there API.

6. This site has the table data for free. If you checkout PHP Classes there are a few different zip locator classes out there.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•