Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
 
Add your own
alternative version

ZIP Code Utility

, 2 Jan 2005 CPOL
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.
#region FILE HEADER
/// <project>ZipCodeUtil</project>
/// <assembly>SagaraSoftware.ZipCodeUtil.dll</assembly>
/// <filename>Location.cs</filename>
/// <creator>Jon Sagara</creator>
/// <description>
/// Contains the Location and LocationInRadius classes.
/// </description>
/// <copyright>
/// Copyright (c) 2004 Sagara Software.  All rights reserved.
/// </copyright>
/// <disclaimer>
/// 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.
/// </disclaimer>
/// <history>
///	<change date="12/29/2004" changedby="Jon Sagara">File created.</changed>
/// </history>
#endregion

using System;
using System.Diagnostics;
using System.Text;

namespace SagaraSoftware.ZipCodeUtil
{
	/// <summary>
	/// A Location is represented by a City, State, ZIP Code, County, Latitude, Longitude, and ZIP 
	///  Class.  This just so happens to correspond to the columns of the ZIP_CODES table.
	/// </summary>
	public class Location
	{
		#region CONSTRUCTORS
		/// <summary>
		/// Default constructor.  Does nothing.
		/// </summary>
		public Location ()
		{
		}
		#endregion


		#region PROPERTIES
		public String City
		{
			get
			{
				return _strCity;
			}
			set
			{
				_strCity = value;
			}
		}

		public String State
		{
			get
			{
				return _strState;
			}
			set
			{
				_strState = value;
			}
		}

		public String ZipCode
		{
			get
			{
				return _strZipCode;
			}
			set
			{
				_strZipCode = value;
			}
		}

		public String County
		{
			get
			{
				return _strCounty;
			}
			set
			{
				_strCounty = value;
			}
		}

		public Double Latitude
		{
			get
			{
				return _dLatitude;
			}
			set
			{
				_dLatitude = value;
			}
		}

		public Double Longitude
		{
			get
			{
				return _dLongitude;
			}
			set
			{
				_dLongitude = value;
			}
		}

		public String ZipClass
		{
			get
			{
				return _strZipClass;
			}
			set
			{
				_strZipClass = value;
			}
		}
		#endregion


		#region METHODS
		public Double DistanceFrom (Location inRemoteLocation)
		{
			return Distance.GetDistance (this, inRemoteLocation);
		}


		public Location[] LocationsWithinRadius (Double inRadius)
		{
			return Radius.LocationsWithinRadius (this, inRadius);
		}


		public override string ToString ()
		{
			StringBuilder str = new StringBuilder ();

			str.AppendFormat ("Location: {0}, {1} {2} in {3} County\n", City, State, ZipCode, County);
			str.AppendFormat ("\tLatitude:\t{0}\n", Latitude);
			str.AppendFormat ("\tLongitude:\t{0}\n", Longitude);
			str.AppendFormat ("\tZip Class:\t{0}\n", ZipClass);

			return str.ToString ();
		}

		#endregion


		#region MEMBER DATA
		private string _strCity;
		private string _strState;
		private string _strZipCode;
		private string _strCounty;
		private double _dLatitude;
		private double _dLongitude;
		private string _strZipClass;
		#endregion
	}


	public class LocationInRadius : Location
	{
		#region CONSTRUCTOR
		public LocationInRadius () : base ()
		{
			DistanceToCenter = Double.MinValue;
		}
		#endregion


		#region PROPERTIES
		public Double DistanceToCenter
		{
			get
			{
				return _dDistToCenter;
			}
			set
			{
				_dDistToCenter = value;
			}
		}
		#endregion


		#region METHODS
		public override string ToString ()
		{
			StringBuilder str = new StringBuilder ();

			str.AppendFormat ("Location: {0}, {1} {2} in {3} County\n", City, State, ZipCode, County);
			str.AppendFormat ("\tLatitude:\t{0}\n", Latitude);
			str.AppendFormat ("\tLongitude:\t{0}\n", Longitude);
			str.AppendFormat ("\tZip Class:\t{0}\n", ZipClass);
			str.AppendFormat ("\tDistance to original location:\t{0}\n", DistanceToCenter);

			return str.ToString ();
		}
		#endregion


		#region MEMBER DATA 
		private double _dDistToCenter;
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jon Sagara
Software Developer (Senior) Sagara Software, Inc.
United States United States
Jon graduated from Cal Poly with a B.S. Computer Engineering. He is currently building a Silverlight-based report scheduling interface for a pharmaceutical reporting company.
 
When he's not fooling around with computers or reading, he's busy spending time with his super wife, Kelly, his two boys, and their rambunctious dog, Homer.
 
Visit my blog
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Jon Sagara
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid