13,295,472 members (62,078 online)

#### Stats

136K views
87 bookmarked
Posted 2 Jan 2005

# ZIP Code Utility

, 2 Jan 2005
This article provides an easy method to lookup a U.S. City/State by ZIP Code, or one or more ZIP Codes by City/State. It also describes a method to calculate the distance between two ZIP Codes and find all other ZIP Codes within a radius of X miles of a specified ZIP Code.
 App.ico bin Release ConsoleTest.exe SagaraSoftware.ZipCodeUtil.dll DataProvider ```#region FILE HEADER /// ZipCodeUtil /// SagaraSoftware.ZipCodeUtil.dll /// Distance.cs /// Jon Sagara /// /// Contains the Distance class. /// /// /// Copyright (c) 2004 Sagara Software. All rights reserved. /// /// /// This file is provided "as is" with no expressed or implied warranty. The author accepts no /// liability for any damage/loss of business that this product may cause. /// /// /// File created. /// #endregion using System; using System.Diagnostics; namespace SagaraSoftware.ZipCodeUtil { /// /// The Distance class takes two objects and /// uses their Latitude and Longitude to determine the distance between them. Uses the /// Haversine formula. /// public class Distance { #region CLASS METHODS /// /// Returns the distance in miles between two locations, calculated using the Haversine /// forumula. /// /// /// /// public static Double GetDistance (Location inLoc1, Location inLoc2) { Debug.Assert (null != inLoc1); Debug.Assert (null != inLoc2); if (null == inLoc1) throw new ArgumentNullException ("inLoc1", "Null location passed in."); if (null == inLoc2) throw new ArgumentNullException ("inLoc2", "Null location passed in."); Debug.Assert (Double.MinValue != inLoc1.Latitude); Debug.Assert (Double.MinValue != inLoc1.Longitude); Debug.Assert (Double.MinValue != inLoc2.Latitude); Debug.Assert (Double.MinValue != inLoc2.Longitude); if (Double.MinValue == inLoc1.Latitude) throw new ArgumentException ("inLoc1.Latitude", string.Format ("The database does not contain latitude information for {0}, {1}.", inLoc1.City, inLoc1.State)); if (Double.MinValue == inLoc1.Longitude) throw new ArgumentException ("inLoc1.Longitude", string.Format ("The database does not contain longitude information for {0}, {1}.", inLoc1.City, inLoc1.State)); if (Double.MinValue == inLoc2.Latitude) throw new ArgumentException ("inLoc2.Latitude", string.Format ("The database does not contain latitude information for {0}, {1}.", inLoc2.City, inLoc2.State)); if (Double.MinValue == inLoc2.Longitude) throw new ArgumentException ("inLoc2.Longitude", string.Format ("The database does not contain longitude information for {0}, {1}.", inLoc2.City, inLoc2.State)); return Haversine (inLoc1, inLoc2); } /// /// /// /// /// /// private static double Haversine (Location inLoc1, Location inLoc2) { /* The Haversine formula according to Dr. Math. http://mathforum.org/library/drmath/view/51879.html dlon = lon2 - lon1 dlat = lat2 - lat1 a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a), sqrt(1-a)) d = R * c Where * dlon is the change in longitude * dlat is the change in latitude * c is the great circle distance in Radians. * R is the radius of a spherical Earth. * The locations of the two points in spherical coordinates (longitude and latitude) are lon1,lat1 and lon2, lat2. */ double dDistance = Double.MinValue; double dLat1InRad = inLoc1.Latitude * (Math.PI / 180.0); double dLong1InRad = inLoc1.Longitude * (Math.PI / 180.0); double dLat2InRad = inLoc2.Latitude * (Math.PI / 180.0); double dLong2InRad = inLoc2.Longitude * (Math.PI / 180.0); double dLongitude = dLong2InRad - dLong1InRad; double dLatitude = dLat2InRad - dLat1InRad; // Intermediate result a. double a = Math.Pow (Math.Sin (dLatitude / 2.0), 2.0) + Math.Cos (dLat1InRad) * Math.Cos (dLat2InRad) * Math.Pow (Math.Sin (dLongitude / 2.0), 2.0); // Intermediate result c (great circle distance in Radians). double c = 2.0 * Math.Atan2 (Math.Sqrt (a), Math.Sqrt (1.0 - a)); // Distance. dDistance = Globals.kEarthRadiusMiles * c; return dDistance; } #endregion } } ```

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

## Share

 Software Developer (Senior) Sagara Software, Inc. United States
Jon is a senior software developer who loves solving problems with the .NET framework.

When he's not fooling around with computers or reading, he's busy spending time with his super wife, Kelly, and his three boys. He also likes to take his mountain bike for a spin.

Visit my blog

 Pro