Find distance between two geo-location in kilometers
public string getDistance(string origin, string destination)
{
System.Threading.Thread.Sleep(1000);
string distance = "";
string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + origin + "&destination=" + destination + "&sensor=false";
string requesturl = url;
string content = fileGetContents(requesturl);
string[] sp = content.Split(' ');
distance = sp[0].ToString();
return distance;
}
protected string fileGetContents(string fileName)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (fileName.ToLower().IndexOf("http:") > -1)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(fileName);
sContents = System.Text.Encoding.ASCII.GetString(response);
}
else
{
System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
sContents = sr.ReadToEnd();
sr.Close();
}
}
catch { sContents = "unable to connect to server "; }
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sContents);
DataSet dsResponse = new DataSet();
dsResponse.ReadXml(new XmlNodeReader(xmlDoc));
if (dsResponse.Tables.Count > 0)
{
if (dsResponse.Tables.Contains("distance"))
{
if (dsResponse.Tables["distance"].Rows.Count > 0)
{
sContents = "";
for (int iCount = 0; iCount < dsResponse.Tables["distance"].Rows.Count; iCount++)
{
sContents = dsResponse.Tables["distance"].Rows[iCount]["text"].ToString();
}
}
}
}
return sContents;
}
call getDistance(location1, location2)
{
System.Threading.Thread.Sleep(1000);
string distance = "";
string url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + origin + "&destination=" + destination + "&sensor=false";
string requesturl = url;
string content = fileGetContents(requesturl);
string[] sp = content.Split(' ');
distance = sp[0].ToString();
return distance;
}
protected string fileGetContents(string fileName)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (fileName.ToLower().IndexOf("http:") > -1)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(fileName);
sContents = System.Text.Encoding.ASCII.GetString(response);
}
else
{
System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
sContents = sr.ReadToEnd();
sr.Close();
}
}
catch { sContents = "unable to connect to server "; }
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sContents);
DataSet dsResponse = new DataSet();
dsResponse.ReadXml(new XmlNodeReader(xmlDoc));
if (dsResponse.Tables.Count > 0)
{
if (dsResponse.Tables.Contains("distance"))
{
if (dsResponse.Tables["distance"].Rows.Count > 0)
{
sContents = "";
for (int iCount = 0; iCount < dsResponse.Tables["distance"].Rows.Count; iCount++)
{
sContents = dsResponse.Tables["distance"].Rows[iCount]["text"].ToString();
}
}
}
}
return sContents;
}
call getDistance(location1, location2)
Comments
Post a Comment