Google API: Calculate travel duration and distance in miles between two locations using C# and VB.Net in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

hi,

i have this xml response how can i assign the values of xml data to label in asp.net c#

https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&destinations=BR7%205QP&key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg

 

Posted one year ago

Hi nauna,

I have created a sample which full fill your requirement you need to modify the code according to your need.

HTML

<table>
    <tr>
        <td>
            Origin Address:
        </td>
        <td>
            <asp:Label ID="lblOriginAddress" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Destination Address:
        </td>
        <td>
            <asp:Label ID="lblDestinationAddress" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Duration Text:
        </td>
        <td>
            <asp:Label ID="lblDurationText" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Duration Value:
        </td>
        <td>
            <asp:Label ID="lblDurationValue" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Distance Text:
        </td>
        <td>
            <asp:Label ID="lblDistanceText" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Distance Value:
        </td>
        <td>
            <asp:Label ID="lblDistanceValue" runat="server" />
        </td>
    </tr>
</table>

C#

protected void Page_Load(object sender, EventArgs e)
{
    string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&destinations=BR7%205QP&key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg";
    WebRequest request = WebRequest.Create(url);
    using (WebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            DataSet dsResult = new DataSet();
            dsResult.ReadXml(reader);
            lblOriginAddress.Text = dsResult.Tables["DistanceMatrixResponse"].Rows[0]["origin_address"].ToString();
            lblDestinationAddress.Text = dsResult.Tables["DistanceMatrixResponse"].Rows[0]["destination_address"].ToString();
            lblDurationText.Text = dsResult.Tables["duration"].Rows[0]["text"].ToString();
            lblDurationValue.Text = dsResult.Tables["duration"].Rows[0]["value"].ToString();
            lblDistanceText.Text = dsResult.Tables["distance"].Rows[0]["text"].ToString();
            lblDistanceValue.Text = dsResult.Tables["distance"].Rows[0]["value"].ToString();
        }
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim url As String = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&destinations=BR7%205QP&key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg"
    Dim request As WebRequest = WebRequest.Create(url)
    Using response As WebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Using reader As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
            Dim dsResult As New DataSet()
            dsResult.ReadXml(reader)
            lblOriginAddress.Text = dsResult.Tables("DistanceMatrixResponse").Rows(0)("origin_address").ToString()
            lblDestinationAddress.Text = dsResult.Tables("DistanceMatrixResponse").Rows(0)("destination_address").ToString()
            lblDurationText.Text = dsResult.Tables("duration").Rows(0)("text").ToString()
            lblDurationValue.Text = dsResult.Tables("duration").Rows(0)("value").ToString()
            lblDistanceText.Text = dsResult.Tables("distance").Rows(0)("text").ToString()
            lblDistanceValue.Text = dsResult.Tables("distance").Rows(0)("value").ToString()
        End Using
    End Using
End Sub

OutPut

Origin Address: Highcombe Cl, London SE9 4QH, UK
Destination Address: Chislehurst BR7 5QP, UK
Duration Text: 8 mins
Duration Value: 493
Distance Text: 3.3 km
Distance Value: 3290

 


Posted one year ago

Hi nauna,

Refer the below sample. You need to convert the km/mtr to miles like below.

HTML

<table>
    <tr>
        <td>
            Origin Address:
        </td>
        <td>
            <asp:Label ID="lblOriginAddress" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Destination Address:
        </td>
        <td>
            <asp:Label ID="lblDestinationAddress" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Duration Text:
        </td>
        <td>
            <asp:Label ID="lblDurationText" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Duration Value:
        </td>
        <td>
            <asp:Label ID="lblDurationValue" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Distance Text:
        </td>
        <td>
            <asp:Label ID="lblDistanceText" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Distance Value:
        </td>
        <td>
            <asp:Label ID="lblDistanceValue" runat="server" />
        </td>
    </tr>
</table>

C#

protected void Page_Load(object sender, EventArgs e)
{
    string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&destinations=BR7%205QP&key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg";
    WebRequest request = WebRequest.Create(url);
    using (WebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            DataSet dsResult = new DataSet();
            dsResult.ReadXml(reader);
            lblOriginAddress.Text = dsResult.Tables["DistanceMatrixResponse"].Rows[0]["origin_address"].ToString();
            lblDestinationAddress.Text = dsResult.Tables["DistanceMatrixResponse"].Rows[0]["destination_address"].ToString();
            lblDurationText.Text = dsResult.Tables["duration"].Rows[0]["text"].ToString();
            lblDurationValue.Text = dsResult.Tables["duration"].Rows[0]["value"].ToString();
            if (dsResult.Tables["distance"].Rows[0]["text"].ToString().Split(' ')[1].ToUpper() == "KM")
            {
                // 1 kilometer is equal to 0.62137119 miles
                lblDistanceText.Text = Math.Round((Convert.ToDouble(dsResult.Tables["distance"].Rows[0]["text"].ToString().Split(' ')[0]) * 0.621371), 2).ToString() + " miles";
            }
            else if (dsResult.Tables["distance"].Rows[0]["text"].ToString().Split(' ')[1].ToUpper() == "M")
            {
                // 1 kilometer is equal to 0.000621371 miles
                lblDistanceText.Text = (Convert.ToDouble(dsResult.Tables["distance"].Rows[0]["text"].ToString().Split(' ')[0]) * 0.000621371).ToString() + " miles";
            }
            lblDistanceValue.Text = dsResult.Tables["distance"].Rows[0]["value"].ToString();
        }
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim url As String = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&destinations=BR7%205QP&key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg"
    Dim request As WebRequest = WebRequest.Create(url)
    Using response As WebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Using reader As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
            Dim dsResult As New DataSet()
            dsResult.ReadXml(reader)
            lblOriginAddress.Text = dsResult.Tables("DistanceMatrixResponse").Rows(0)("origin_address").ToString()
            lblDestinationAddress.Text = dsResult.Tables("DistanceMatrixResponse").Rows(0)("destination_address").ToString()
            lblDurationText.Text = dsResult.Tables("duration").Rows(0)("text").ToString()
            lblDurationValue.Text = dsResult.Tables("duration").Rows(0)("value").ToString()
            If dsResult.Tables("distance").Rows(0)("text").ToString().Split(" "c)(1).ToUpper() = "KM" Then
                ' 1 kilometer is equal to 0.62137119 miles
                lblDistanceText.Text = Math.Round((Convert.ToDouble(dsResult.Tables("distance").Rows(0)("text").ToString().Split(" "c)(0)) * 0.621371), 2).ToString() + " miles"
            ElseIf dsResult.Tables("distance").Rows(0)("text").ToString().Split(" "c)(1).ToUpper() = "M" Then
                ' 1 kilometer is equal to 0.000621371 miles
                lblDistanceText.Text = (Convert.ToDouble(dsResult.Tables("distance").Rows(0)("text").ToString().Split(" "c)(0)) * 0.000621371).ToString() + " miles"
            End If
            lblDistanceValue.Text = dsResult.Tables("distance").Rows(0)("value").ToString()
        End Using
    End Using
End Sub

Output

Origin Address: Highcombe Cl, London SE9 4QH, UK
Destination Address: Chislehurst BR7 5QP, UK
Duration Text: 8 mins
Duration Value: 493
Distance Text: 2.05 miles
Distance Value: 3290