Zip Code Radius Search in PHP?

Status
Not open for further replies.

krazyjosh5

theres GOLD in dem tubes!
May 28, 2007
1,010
19
0
Hey all,
Im opening up a locator site (ie type in your zip code/city, these shops are near by) and Im trying to figure out how Id do 'Search for shops within 5/10/15/20 miles from this zipcode. Any ideas on that? Im definitely not trying to puchase a database or API to do that.

Also, would this put a strain on my server (im using shitty shared hosting... gonna wait til I see profits before investing in VPS or higher) to search for the zip codes close to the inputted one and then REsearch through the database of shops to identify any shops matching a nearby zip code?

Thanks! :bowdown:
 


1. I believe zipcodes are based on Lat/long so you may be able to do some calculating.

2. The speed/efficiency of these lookups is going to depend a lot on how you've got your DB schema setup, the queries you're running and how you're handling the data when you get to PHP.
 
The zip code databases I've used include latitude/longitude for the centriod of the zip code area, and given a pair of lat/lon values there's a formula to calculate the distance called great circle (I think). If you're just talking about getting a rough approximation then it's possible to use some other equations that are "cheaper" in terms of CPU load which are close enough for government work. :)

If all you care about is zip code matching, then that should not tax your database at all, if it's set up properly.
 
Just a guess...I know that the neighboring towns in my area are very small in terms of zip code differential (***60 - ***50). This is a distance of apx. 3 miles. Therefore, one can assume that you increment the code by 1 for every .3 miles IN MY AREA. You could apply this, but it would be highly unorthidox and will probably not work in all areas.

Jason
 
eh. like i wanna make a shop finder but jesus, its useless without a 'within x miles' capability.. this seems harder than i imagined it to be
 
A little math never hurt anybody. :)

PHP Help: Great Circle Distance Function

If you don't care that much about accuracy, you can do something like 0.5*(abs(lat1 - lat2) + abs(lon1 - lon2)) to get a number you can use to see how far things are relative to each other (but it's not in miles or any meaningful unit).

I've done something like this to match up locations of airports to cities, for example, and it works just fine. I just needed to know if it was close, not that it was 10.5455 miles away.
 
Status
Not open for further replies.