Zip Code Locator in PHP [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the questio开发者_高级运维n so it can be answered with facts and citations.
Closed 6 years ago.
Improve this questionI was thinking about using either the google or yahoo api to calculate the distance from one zip code to another, and to get the city of that zip code. However, the api calls are limited, as the website I am working on will query the api multiple times throughout multiple pages.
I was wondering where can I go for either a database with zip codes and cities, or a zip code database to query lat / long for distance.
I did some googling, and most of the free ones I downloaded were either not accurate, or it was to large to fit in the database.
Thanks
I will be using PHP
In the past I have used this PHP class. While I haven't used it very extensively, it did what I needed it to do in terms of Zip Code lookup and distance.
Commercial zip code databases with Lat/Long are available. They are not expensive and are not large (well, if you restrict to USA, 40K small records or so). I have had good luck with zip-finder.com in the past, but important caveat... once you begin maintaining your own zip code table(s), you will need to keep it in sync with whatever the USPS does with zipcodes over time. One really irritating thing they do is remove zipcodes.
That said, calculating distance is pretty trivial, but you only get one lat/long point per zipcode (more or less the centroid of area). For a large zipcode, your distance accuracy can have a mile or more of slop in it, so be aware of that.
This is the best free zip code database you will find: http://federalgovernmentzipcodes.us/
It works fine if you don't need super accurate lat,lng. Unfortunately the lat,lng are not that accurate, not because of the two decimal places - but rather because they are simply a bit off.
I reworked this database to make the lat,lng more accurate by hitting google maps
maps.googleapis.com/maps/api/geocode/json?address="zip code city state";
You can try this API
http://ws.geonames.org/postalCodeSearch?postalcode=10033
Gives out results with lat and long too
Check out PHP-ZipCode-Class. You can adapt it to any number of zip code databases. Personally, I would go with a commercial database as the free ones can easily get outdated. Trust me on that. I tried to maintain a free database on a high traffic e-commerce site for years. Would have been a LOT CHEAPER to just buy a commercial database. If you really insist on a free database, here are a couple that I know of (but have not tried).
- http://zipcode.jassing.com/
- http://federalgovernmentzipcodes.us/
You're correct about the api limitations. It is true with Bing as well (although their api is pretty good).
If you go with a database... Although it takes a little work, you can get it free from the US Census Tiger Data - which is what most low-end 3rd party ZIP Code database are based on. Just know that in 2000, they replaced the ZIP Code with ZCTA (which is ZIP Code like, but not exact). I’ve included the link below which has an explanation of ZCTA from the census site: http://www.census.gov/geo/ZCTA/zcta.html
Other things to consider: most latitude and longitude centroids are based on geometric calculations- meaning they could fall in the middle of forestry land, large lakes, parks (e.g. Central Park) where no people live. I’m not sure what your needs are but that may be fine for you. If you need population based centers, you will probably need commercial data (see http://greatdata.com/zip-codes-lat-long click the ‘more details’ tab at the top for an explanation of this topic).
Also, determine if you only need the major city for each ZIP Code (one-to-one relationship – normally 40,000+ records) or if a ZIP Code boundary covers more than one city, you need each city listed as a separate record (~57,000 records). Most locators and address validation utilities need the latter.
I've been using the zip-code database from http://zip-info.com/ for many years. It's updated every quarter (very important) and is very accurate. The database is about $50 and I purchase an update twice a year.
There are something like 54,000 5 digit zip codes in the US - so any good database is going to be large - just strip out the data fields you don't need (limit it to zip/lat/lon) if you want to reduce data storage (though it's minimal savings). As Rob said, distance calcs are easy to do - just look for a script that does great circle calculations as a staring point.
精彩评论