Calculate Distance, Duration between two locations in Google Maps API using C# and VB.Net in ASP.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

hello,

i am using this google map api on my page behind to calculate the distance but it is showing incorrect distance in KM please check and advice

                string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins="+origin.Text+"&destinations=" + Label9.Text + "&key=AIzaSyBE1J5Pe_GZXBR_x9TXOv6TU5vtCSmEPW4";
                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();
                        duration.Text = dsResult.Tables["duration"].Rows[0]["text"].ToString();
                        distance.Text = dsResult.Tables["duration"].Rows[0]["value"].ToString() + dsResult.Tables["distance"].Rows[0]["text"].ToString();
                    }
                }

 

 

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago
nauna says:
distance.Text = dsResult.Tables["duration"].Rows[0]["value"].ToString() + dsResult.Tables["distance"].Rows[0]["text"].ToString();

Replace the above line with below.

distance.Text = dsResult.Tables["distance"].Rows[0]["text"].ToString();

Check the below sample code.

HTML

<div>
    duration;
    <asp:TextBox runat="server" ID="duration" />
    Distance:
    <asp:TextBox ID="distance" runat="server" />
</div>

Namespaces

C#

using System.Net;
using System.IO;
using System.Text;
using System.Data;

VB.Net

Imports System.Net
Imports System.IO
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string origin = "Oberoi Mall, Goregaon";
    string destination = "Infinity IT Park, Malad East";
    string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + origin + "&destinations=" + destination + "&key=AIzaSyBE1J5Pe_GZXBR_x9TXOv6TU5vtCSmEPW4";
    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);
            duration.Text = dsResult.Tables["duration"].Rows[0]["text"].ToString();
            distance.Text = dsResult.Tables["distance"].Rows[0]["text"].ToString();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim origin As String = "Oberoi Mall, Goregaon"
    Dim destination As String = "Infinity IT Park, Malad East"
    Dim url As String = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & origin & "&destinations=" & destination & "&key=AIzaSyBE1J5Pe_GZXBR_x9TXOv6TU5vtCSmEPW4"
    Dim request As WebRequest = WebRequest.Create(url)
    Using response As WebResponse = CType(request.GetResponse(), HttpWebResponse)
        Using reader As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.UTF8)
            Dim dsResult As DataSet = New DataSet()
            dsResult.ReadXml(reader)
            duration.Text = dsResult.Tables("duration").Rows(0)("text").ToString()
            distance.Text = dsResult.Tables("distance").Rows(0)("text").ToString()
        End Using
    End Using
End Sub