We have created a demo window application with two textboxes for accepting zipcodes. And on basis of those zip code values we are picking latitude and longitude from database. And then calculating distance between them.
. The database is in .csv format. I opened it in Excel and saved it as .xlsx. Imported the .xlsx in Access database & saved in table named
. There are two sized database(s).
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
namespace ZipDistancer
{
public class ZipCode
{
private string _code;
private string _state;
private double _latitude;
private double _longitude;
public string Code
{
get { return _code; }
set { _code = value; }
}
public string State
{
get { return _state; }
set { _state = value; }
}
public double Longitude
{
get { return _longitude; }
set { _longitude = value; }
}
public double Latitude
{
get { return _latitude; }
set { _latitude = value; }
}
#region Static methods/variables
private static List<ZipCode> _codeList;
public static List<ZipCode> CodeList
{
get { return _codeList; }
set { _codeList = value; }
}
public static void LoadData()
{
using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ZipCon"].ConnectionString))
{
string query = "SELECT ZipCode, State, Latitude, Longitude FROM zipcodes";
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
con.Open();
using (OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
DataTable dt = reader.GetSchemaTable();
_codeList = new List<ZipCode>();
string latitude, longitude;
while (reader.Read())
{
ZipCode code = new ZipCode();
code._code = reader[0].ToString();
code._state = reader[1].ToString();
latitude = reader[2].ToString(); longitude = reader[3].ToString();
if (!String.IsNullOrEmpty(latitude) && !String.IsNullOrEmpty(longitude))
{
code.Latitude = Convert.ToDouble(latitude);
code.Longitude = Convert.ToDouble(longitude);
}
_codeList.Add(code);
}
reader.Close();
}
con.Close();
}
}
}
public static double Distance(string zipCode1,
string zipCode2)
{
ZipCode z1 = _codeList.Find(
delegate(ZipCode z)
{
return z.Code == zipCode1;
});
ZipCode z2 = _codeList.Find(
delegate(ZipCode z)
{
return z.Code == zipCode2;
});
if (z1 == null || z2 == null)
throw new ArgumentException
("One of the codes does not exist.");
double earthsRadius = 3956.087107103049;
double latitude1Radians =
(z1.Latitude / 180) * Math.PI;
double longitude1Radians =
(z1.Longitude / 180) * Math.PI;
double latitude2Radians =
(z2.Latitude / 180) * Math.PI;
double longitude2Radians =
(z2.Longitude / 180) * Math.PI;
double distance =
(earthsRadius * 2) *
Math.Asin(
Math.Sqrt(
Math.Pow(
Math.Sin((latitude1Radians -
latitude2Radians) / 2), 2) +
Math.Cos(latitude1Radians) *
Math.Cos(latitude2Radians) *
Math.Pow(
Math.Sin((longitude1Radians -
longitude2Radians) / 2), 2)
)
);
return distance;
}
#endregion
}
}
And here is the zip file of entire project.
class instance. Hope this helps. Let us know the action result.