Function for returning "nearest cities" ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nou
Forum Newbie
Posts: 8
Joined: Tue Oct 25, 2011 3:21 am

Function for returning "nearest cities" ?

Post by nou »

I already have a MYSQL database with table ZIP_CODES, and columns zip, state (2 letters), city, longitude, latitude, and full_state

My aim is to have a form where people can search for other users' listings by city or zip code, have those results returned, and have the option to show more listings within 10, 15, 25 etc miles. I've been searching all over these forums+google and everything seems to be outdated or not working.

I'm guessing that a function could be created that handled this math, some complex algorithm or maybe not. Does anyone have any experience with this? thanks.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Function for returning "nearest cities" ?

Post by twinedev »

I have used some (in fact it was a site that was 8 years old, so not sure how you get "Outdated" calculations when it is the same Long/Lat we've had before programming).

I looked myself a few years back, and found there are a few different methods, the more complex ones are more precise. I never got around it, but instead of searching across ALL of the zip code table, I wanted to write a system that figured out a from the given zip code, what would be the max/min long/lat values from the point, put those into a temporary table, then do the query against the temp table. So now instead of querying (which involved calculating values from the tables) ALL zip codes, just do the ones within a a square around the starting point.

Again, never got around to trying it, and don't have access to backups of when I was working on it.

-Greg
Post Reply