Zip Code Database |
||||||||||
|
There are many out there who need access to zip code data. Free-ZipCodes.com answers that call. We provide you with the zip code database as well as show you how to use it. Zip Code DataThe schema of the data is as follows:Zipcode||Latitude||Longitude||State||City||County Here is a sample record, double-pipe delimited: 32801||28.539882||-81.372668||FL||ORLANDO||ORANGE You'll want to process the text file and store it in a database or use it 'as is' as a flat file. A Word On QualityOur free zip code database is from 2006. If you want up-to-date zip code data we recommend you research the many commercial vendors. Typically, you'll pay anywhere from $5 to $50. What you get for the money is more meta data such as population, average income, area codes and more. Definitely worth exploring before you commit to a schema.If you are ok with basic data that is slightly out of date, then go ahead and download our free offering. Format: zip File size: 680 KB Records: 41700 zip codes
Using the Zip Code DatabaseThe three most popular zip code applications are:1. Calculating the distance between two zipcodes ("As the crow flies") 2. Finding a list of zip codes within a specified radius of a zip code 3. Programming with the Google Maps API Application: As The Crow FliesTo calculate the distance between two points on a sphere, you need the latitude and longitude for the two zipcodes and a little bit of math wizardry.For most applications it's not important to understand how this works unless you specialize in geophysics. Our example uses Perl code (see zip_distance.pl), but you can port to whatever programming language you like. 1. Define two constants (refers to the size of our great planet):$earth_radius = 3959; # average radius of the earth$rads = 57.2958; # 180/pi, used to convert latitude/longitude from degrees to radians Those constants work for miles. You would have to change them for a different unit of measurement such as kilometers. 2. Get the Latitude and Longitude of two Zip CodesAssuming you have the data in a database, do a select like this:($lat1,$lon1) = $dbh->selectrow_array("select latitude,longitude from zipcodes where zipcode = $zip1"); ($lat2,$lon2) = $dbh->selectrow_array("select latitude,longitude from zipcodes where zipcode = $zip2"); 3. Calculate the distance between two points in milesUsing the lattitude/longitude data and the constants defined above, you are ready to run the calculation.# calculate the distance(miles) between two sets of longitude/latitude coordinates $x = (sin($lat1/$rads) * sin($lat2/$rads)) + (cos($lat1/$rads) * cos($lat2/$rads) * cos(($lon2/$rads) - ($lon1/$rads))); $miles = $earth_radius * atan2(sqrt(1 - ($x**2)),$x); # round float to 1 decimal place $miles = sprintf("%.1f",$miles); 4. Print the data!That was easy!print "The distance between $zip1 and $zip2 is $miles miles."; Try out our PHP version (enter one zip code in each field): Application: Finding Zip Codes Within a Fixed RadiusDisplaying zip codes within a certain radius from a fixed zip code requires a little bit of overage. This is because zip codes are a single point while many people can live within a zip code. For most applications we are interested in all people/places within a zip code, not just the exact distance from the latitude/longitude of a zip code.This fact makes our job a little easier. All we have to do is define a rectangle that is slightly bigger than the circumference of the area we are searching. To illustrate this, we tell the user we are searching for people within the red circle. In fact, we are returning all zip codes within the green square. ![]() One more bit of information and we're set. Latitude and longitude are expressed as degrees. One degree latitude is approximately 69.172 miles. Longitude distance changes as you get closer to the poles, so we have to do a little extra as you'll see in the code example. In general we can see that one mile is 1/69.172 degrees. So let's try to find all the zip codes within a five mile radius (5/69.172 = 0.0723 degrees). You can view the Perl code here (zip_range.pl) First, we need the latitude, longitude of the fixed point. ($lat,$lon) = $dbh->selectrow_array("select latitude,longitude from zipcodes where zipcode = $zip"); Next, we find the range. $lat_range = $range/69.172; $lon_range = abs($range/(cos($lon) * 69.172)); # here we do a little extra for longitude $min_lat = $lat - $lat_range; # $mile_radius would be 5 for our example, $lat is the fixed latitude for our zip code $max_lat = $lat + $lat_range; $min_lon = $lon - $lon_range; $max_lon = $lon + $lon_range; And now we can run the SQL to find the zip codes within the specified range. We will store them in an array. $query = "select zipcode from zipcodes where (latitude BETWEEN $min_lat AND $max_lat) AND (longitude BETWEEN $min_lon AND $max_lon"; $sth = $dbh->prepare($query); $sth->execute(); while ( @row = $sth->fetchrow_array( ) ) { push(@zip_in_range,$row[0]); } That should do it. Just print the results: foreach (@zip_in_range) { print $_ . "\n"; } Application: Google Maps APIWith the Google Maps API you can dynamically input data (into Javascript/Ajax stuff) and create markers on the map. This is beyond the scope of our simple tutorial so visit the Google Maps API for the details. Fun stuff."Strange is our situation here upon earth. Each of us comes for a short visit, not knowing why, yet sometimes seeming to a divine purpose. From the standpoint of daily life, however, there is one thing we do know: That we are here for the sake of others...for the countless unknown souls with whose fate we are connected by a bond of sympathy. Many times a day, I realize how much my outer and inner life is built upon the labors of people, both living and dead, and how earnestly I must exert myself in order to give in return as much as I have received." - Albert Einstein |
|
||||||||