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

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.
using System;
using SagaraSoftware.ZipCodeUtil;

namespace ConsoleTest
{
	/// <summary>
	/// A test class to demonstrate the usage of the ZipCodeUtil library.
	/// </summary>
	class Driver
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main (string[] args)
		{
			//	Location by ZIP Code.
			Location location = ZipCodeUtil.LookupByZipCode ("93275");
			if (null != location)
				Console.WriteLine (location.ToString ());

			//	Location(s) by City/State.
			Location[] locs = ZipCodeUtil.LookupByCityState ("Tulare", "CA");
			if (null != locs && locs.Length > 0)
			{
				foreach (Location loc in locs)
				{
					Console.WriteLine (loc.ToString ());
				}
			}

			//	Location by City/State/Zip
			location = ZipCodeUtil.LookupByCityStateZip ("Tulare", "CA", "93275");
			if (null != location)
				Console.WriteLine (location.ToString ());

			//	Distance between two locations.
			Location sf = ZipCodeUtil.LookupByZipCode ("94175");
			Location la = ZipCodeUtil.LookupByZipCode ("90185");
			Double dDistance = sf.DistanceFrom (la);
			Console.WriteLine ("{0} is {1} miles from {2}", sf.City, dDistance, la.City);

			//	Other Locations within an X-mile radius of a specific location.
			locs = sf.LocationsWithinRadius (5.0);
			if (null != locs && locs.Length > 0)
			{
				foreach (Location loc in locs)
				{
					Console.WriteLine (loc.ToString ());
				}
			}


			Console.ReadLine ();
		}
	}
}

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 | Mobile
Web03 | 2.8.140922.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Jon Sagara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid