Question

I have a location (latitude & longitude). How can I get a list of zipcodes that are either partially or fully within the 10 mile radius of my location?

The solution could be a call to a well known web service (google maps, bing maps, etc...) or a local database solution (the client has sql server 2005) or an algorithm.

I have seen the somewhat similar question, but all the answers there pretty much pertain to using SQL Server 2008 geography functionality which is unavailable to me.

Was it helpful?

Solution

Firstly, you'll need a database of all zipcodes and their corresponding latitudes and longitudes. In Australia, there are only a few thousand of these (and the information is easily available), however I assume it's probably a more difficult task in the US.

Secondly, given you know where you are, and you know the radius you are looking for, you can look up all zipcodes that fall within that radius. Something simple written in PHP would be as follows: (apologies it's not in C#)

function distanceFromTo($latitude1,$longitude1,$latitude2,$longitude2,$km){
  $latitude1  = deg2rad($latitude1);
  $longitude1 = deg2rad($longitude1);
  $latitude2  = deg2rad($latitude2);
  $longitude2 = deg2rad($longitude2);
  $delta_latitude  = $latitude2  - $latitude1;
  $delta_longitude = $longitude2 - $longitude1;
  $temp = pow(sin($delta_latitude/2.0),2) + cos($latitude1) * cos($latitude2) * pow(sin($delta_longitude/2.0),2);
  $earth_radius = 3956;
  $distance = $earth_radius * 2 * atan2(sqrt($temp),sqrt(1-$temp));
  if ($km)
    $distance = $distance * 1.609344;
  return $distance;
}

OTHER TIPS

Start with a zip code database that contains zipcodes and their corresponding latitude and longitude coordinates:

http://www.zipcodedownload.com/Products/Product/Z5Commercial/Standard/Overview/

To get the distance between latitude and longitude, you will need a good distance formula. This site has a couple variations:

http://www.meridianworlddata.com/distance-calculation/

The "Great Circle Distance" formula is a little extreme. This one works well enough from my experience:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1)
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

Your SQL Query will then look something like this:

select zd.ZipCode
from ZipData zd
where 
    sqrt(
        square(69.1 * (zd.Latitude - @Latitude)) +
        square(69.1 * (zd.Longitude - @Longitude) * cos(@Latitude/57.3))
    ) < @Distance

Good luck!

Most searches work with centroids. In order to work with partial zipcodes being within the 10 miles, you are going to have to buy a database of zipcode polygons (*). Then implement an algorithm which checks for zipcodes with vertices within your 10 mile radius. To be done properly, you owuld use the Haversine formula for the distance measurement. With some clever data structures, you can significant reduce the search space. Similarly, searches can be greatly speeded up by storing and initially comparing against zipcoe extents (North,West,East,South).

(*) Note: Technically zipcodes are NOT polygons! I know we all think of them like that, but really they are collections of data points (street addresses) and this is how the USPS really uses them. This means zipcodes can include other zipcodes; zipcodes can be made of multiple "polygons"; and zipcodes can overlap other zipcodes. Most of these situations should not be a problem, but you will have to handle zipcodes that can be defined as multiple polygons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top