Page 1 of 1

Function for returning "nearest cities" ?

Posted: Wed Oct 26, 2011 11:27 pm
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.

Re: Function for returning "nearest cities" ?

Posted: Thu Oct 27, 2011 12:45 am
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