Here’s a neat little method I figured out for finding geographic points (waypoints) located within a certain radius of a “master point.” To make this a bit more concrete, let’s say you want to find all the zip codes within a 10 mile radius of your zip code, 27701 (Durham, NC). Most good zip code databases will have the latitude and longitude coordinates (waypoints) of each zip code, but this is alot of data (my database has over 79,000 zip code entries). It is far too much data to calculate a distance between the master point and each zip code so you need to find a way to quickly zoom in on the points that are most likely to be within your radius.

I’ve included the image above to help in this illustration. In this image X marks my master point, Durham, NC. The various green stars represent other zip codes around Durham. The first step is to determine the boundaries of our radius. A good rule of thumb to use is to assume 1 degree of longitude is about 70 miles wide while a degree of latitude is about 50 miles (this isn’t exact but we’ll improve this estimate when we calculate the actual radius).
In this case, our target zip codes will be between longitudes ‘a’ and ‘b’ while the latitudes will be between ‘c’ and ‘d.’ These can be calculated easily by adding and subtracting our radius ‘r’ from our center point X to get each limit. We can now query our database to return just those zipcodes where the latitudes are between ‘c’ and ‘d’ and longitudes are between ‘a’ and ‘b’ (example: SELECT * FROM zip WHERE latitude >= ‘c’ AND latitude <= ‘d’ AND longitude >= ‘a’ AND longitude <= ‘b’)This query returns 8 points but a few of these are outside our 10 mile radius we originally set. But now we’ve narrowed our consideration set down from 79,000 to just 10 points. We can calculate the distance between X and each of these points to see if they lie within the radius ‘r,’ leaving those points within the shaded circle as our result.The equation I use to calculate distances between two waypoints is:
$radius = 3959 * acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($long2 - $long1)); where $radius is in miles
There are a number of ways to calculate these distances that can be as complicated as you like but again, this should be close enough for most applications. Realize, however, that if you try to make this more precise by using a more complicated equation, you’ll severely increase the processing time required to calculate distances. Also note that increasing the radius under consideration can exponentially increase processing times depending on the nature and size of your dataset.
Many companies offer scripts for sale that calculate zip code radiuses and distances but with a little work and using this framework, you can create your own application. Take a look at an example of my implementation at: singletracks.