Hi Aashish682,
I have created sample by modifying your code. Refer the below code.
HTML
<asp:Label ID="lblDistance" runat="server" />
Code
C#
using System;
using System.IO;
using System.Net;
using System.Xml;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string origin = GetLocation(18.641400, 72.872200);
string destination = GetLocation(18.964700, 72.825800);
lblDistance.Text = GetDrivingDistanceInMiles(origin, destination).ToString();
}
/// <summary>
/// Get Driving Distance In Miles based on Source and Destination.
/// </summary>
/// <param name="origin"></param>
/// <param name="destination"></param>
/// <returns></returns>
public double GetDrivingDistanceInMiles(string origin, string destination)
{
string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + origin + "&destinations=" + destination + "&mode=driving&sensor=false&language=en-EN&units=imperial";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader sreader = new StreamReader(dataStream);
string responsereader = sreader.ReadToEnd();
response.Close();
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(responsereader);
if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
{
XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
return Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" mi", ""));
}
return 0;
}
/// <summary>
/// Get Location based on Latitude and Longitude.
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <returns></returns>
public string GetLocation(double latitude, double longitude)
{
string url = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader sreader = new StreamReader(dataStream);
string responsereader = sreader.ReadToEnd();
response.Close();
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(responsereader);
if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
{
XmlNodeList location = xmldoc.GetElementsByTagName("distance");
return xmldoc.GetElementsByTagName("formatted_address")[0].ChildNodes[0].InnerText;
}
return "";
}
}
VB.Net
Imports System.Net
Imports System.IO
Imports System.Xml
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim origin As String = GetLocation(18.6414, 72.8722)
Dim destination As String = GetLocation(18.9647, 72.8258)
lblDistance.Text = GetDrivingDistanceInMiles(origin, destination).ToString()
End Sub
''' <summary>
''' Get Driving Distance In Miles based on Source and Destination.
''' </summary>
''' <param name="origin"></param>
''' <param name="destination"></param>
''' <returns></returns>
Public Function GetDrivingDistanceInMiles(origin As String, destination As String) As Double
Dim url As String = (Convert.ToString((Convert.ToString("https://maps.googleapis.com/maps/api/distancematrix/xml?origins=") & origin) + "&destinations=") & destination) + "&mode=driving&sensor=false&language=en-EN&units=imperial"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim sreader As New StreamReader(dataStream)
Dim responsereader As String = sreader.ReadToEnd()
response.Close()
Dim xmldoc As New XmlDocument()
xmldoc.LoadXml(responsereader)
If xmldoc.GetElementsByTagName("status")(0).ChildNodes(0).InnerText = "OK" Then
Dim distance As XmlNodeList = xmldoc.GetElementsByTagName("distance")
Return Convert.ToDouble(distance(0).ChildNodes(1).InnerText.Replace(" mi", ""))
End If
Return 0
End Function
''' <summary>
''' Get Location based on Latitude and Longitude.
''' </summary>
''' <param name="latitude"></param>
''' <param name="longitude"></param>
''' <returns></returns>
Public Function GetLocation(latitude As Double, longitude As Double) As String
Dim url As String = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=" & latitude & "," & longitude & "&sensor=false"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim sreader As New StreamReader(dataStream)
Dim responsereader As String = sreader.ReadToEnd()
response.Close()
Dim xmldoc As New XmlDocument()
xmldoc.LoadXml(responsereader)
If xmldoc.GetElementsByTagName("status")(0).ChildNodes(0).InnerText = "OK" Then
Dim location As XmlNodeList = xmldoc.GetElementsByTagName("distance")
Return xmldoc.GetElementsByTagName("formatted_address")(0).ChildNodes(0).InnerText
End If
Return ""
End Function
End Class
Output
67.9