Calculating the distance between ZIP codes has become a common feature in search engines. For instance, if you're searching for restaurants, a Web site will often allow you to enter a ZIP code and display all restaurants within x miles of that ZIP code. Zach Smith shows you how to implement a ZIP code distance calculator in C#.
Calculating the distance between ZIP codes has become a common feature in search engines. For instance, if you're searching for restaurants, a Web site will often allow you to enter a ZIP code and display all of the restaurants within X miles of that ZIP code. Here is one way application developers to implement a ZIP code distance calculator using the .NET Framework and C#.
The TechRepublic Download version of this blog post includes a PDF version of the article as well as a Visual Studio project file that contains all of the code for the ZIP code calculator.
Getting the data
Before starting we need to have a list of all ZIP codes and their respective latitude/longitude coordinates. You can get this data from the US Census Bureau -- the data that I'm using is from the 2000 census.
On that page the data is available for download by clicking the "ZCTAs (ZIP Code Tabulation Area)" link under the "ASCII text versions of" heading. This file includes information on all ZIP codes, including population numbers, latitude/longitude coordinates, housing information, geography data, and state in which the ZIP code resides.
You will need to take this data and either import it into a database or transform it into an XML document.
Using the data
I am using an XML version of the data which can be deserialized into a generic List<T> object. This allows me to search the list and retrieve the latitude/longitude coordinates for the distance calculations. I did this by parsing the list provided by the Census Bureau and inserting the data I needed into a custom C# class called ZIPCode. The ZIPCode class contains the following:
- Instance Properties
- State - Abbreviation for the state which the ZIP code is in.
- Code - The five digit ZIP code
- Latitude - The latitude of the ZIP code
- Longitude - The longitude of the ZIP code
- Static Properties and Methods
- CodeList - A List<ZIPCode> property containing all the ZIP codes
- LoadData - A method that loads all of the ZIP code data into the CodeList property
- Distance - A method that returns the distance between two ZIP codes as a type double.
What the XML looks like
The deserialization codeOnce we have the List object populated with data we can begin to search through it and access the data. The searching will be done by utilizing the List<T>.Find method as shown in Figure C.
The Find method
This code searches through the _codeList variable and returns the first instance where z.Code equals ZIPCode1, which is a string representing the ZIP code to find. Once we've done this, we do the same thing for the other ZIP code. We then use the latitude and longitude coordinates from those objects to calculate the distance.
Calculating the distance
This article uses a derivation of the Haversine formula to calculate the distance between ZIP codes. The Haversine formula is used to determine the distance between two points on a sphere - in this case the earth. While this isn't 100 percent accurate due to the earth not being a perfect sphere, it is probably as close as you're going to get without getting out the tape measure!
I'm not going to go into the math behind the Haversine formula, but if you would like to learn more here is another good resource. This second link is particularly interesting as it gives a few alternatives to the Haversine formula.Figure D shows the complete function used to calculate the distance between two ZIP codes in C#.
The complete function
This method accepts two ZIP codes and retrieves the data for them from the List<T> object. It then converts the latitude/longitude points to radians and feeds those values into the distance formula. The result of this formula is the distance, in miles, between the two points.
The TechRepublic Download
Please take advantage of the TechRepublic Download that is associated with this article. The download includes a PDF version of the article as well as a Visual Studio project file that contains all of the code for the ZIP code calculator.