Get Real Time Distance and ETA in asp.net using Google API
Get Real Time Distance and ETA in asp.net using Google API
C# Code :
public bool Getdistance()
{
string result = "";
WebRequest request = null;
HttpWebResponse response = null;
String url = "https://maps.googleapis.com/maps/api/directions/xml?origin=28.668186,77.2301363&destination=28.603156,77.1773745";
request
= WebRequest.Create(url);
response
= (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new
System.IO.StreamReader(stream, ec);
result
= reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(result); // suppose that str string contains
"<Names>...</Names>"
XmlNodeList xnListDuration = xml.SelectNodes("/DirectionsResponse/route/leg/duration");
foreach (XmlNode xn in xnListDuration)
{
string val = xn.LastChild.InnerText;
Response.Write("Duration : " + val.ToString());
}
XmlNodeList xnListDistance = xml.SelectNodes("/DirectionsResponse/route/leg/distance");
foreach (XmlNode xn in xnListDistance)
{
string val = xn.LastChild.InnerText;
Response.Write("Distance : " + val.ToString());
}
return true;
}
Comments
Post a Comment