ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Google API: Calculate travel duration and distance in miles between two locations using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>I have created a sample which full fill your requirement you need to modify the code according to your need.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;table&gt; &lt;tr&gt; &lt;td&gt; Origin Address: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblOriginAddress&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Destination Address: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDestinationAddress&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Duration Text: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDurationText&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Duration Value: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDurationValue&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Distance Text: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDistanceText&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Distance Value: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDistanceValue&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { string url = &#34;https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&amp;destinations=BR7%205QP&amp;key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg&#34;; 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[&#34;DistanceMatrixResponse&#34;].Rows[0][&#34;origin_address&#34;].ToString(); lblDestinationAddress.Text = dsResult.Tables[&#34;DistanceMatrixResponse&#34;].Rows[0][&#34;destination_address&#34;].ToString(); lblDurationText.Text = dsResult.Tables[&#34;duration&#34;].Rows[0][&#34;text&#34;].ToString(); lblDurationValue.Text = dsResult.Tables[&#34;duration&#34;].Rows[0][&#34;value&#34;].ToString(); lblDistanceText.Text = dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;text&#34;].ToString(); lblDistanceValue.Text = dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;value&#34;].ToString(); } } }</pre> <p><strong><span style="text-decoration: underline;">VB.Net</span></strong></p> <pre class="brush: vb">Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim url As String = &#34;https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&amp;destinations=BR7%205QP&amp;key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg&#34; 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(&#34;DistanceMatrixResponse&#34;).Rows(0)(&#34;origin_address&#34;).ToString() lblDestinationAddress.Text = dsResult.Tables(&#34;DistanceMatrixResponse&#34;).Rows(0)(&#34;destination_address&#34;).ToString() lblDurationText.Text = dsResult.Tables(&#34;duration&#34;).Rows(0)(&#34;text&#34;).ToString() lblDurationValue.Text = dsResult.Tables(&#34;duration&#34;).Rows(0)(&#34;value&#34;).ToString() lblDistanceText.Text = dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;text&#34;).ToString() lblDistanceValue.Text = dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;value&#34;).ToString() End Using End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">OutPut</span></strong> </p> <table border="1"> <tbody> <tr> <td><strong>Origin Address:</strong></td> <td>Highcombe Cl, London SE9 4QH, UK</td> </tr> <tr> <td><strong>Destination Address:</strong></td> <td>Chislehurst BR7 5QP, UK</td> </tr> <tr> <td><strong>Duration Text:</strong></td> <td>8 mins</td> </tr> <tr> <td><strong>Duration Value:</strong></td> <td>493</td> </tr> <tr> <td><strong>Distance Text:</strong></td> <td>3.3 km</td> </tr> <tr> <td><strong>Distance Value:</strong></td> <td>3290</td> </tr> </tbody> </table> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/Mon, 02 Oct 2017 01:42:15 GMTGoogle API: Calculate travel duration and distance in miles between two locations using C# and VB.Net in ASP.Net<p>hello,</p> <p>&nbsp;</p> <p>it works fine, can you we get the distance in miles instead of km please advice thanks</p>https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/Mon, 02 Oct 2017 22:02:28 GMTGoogle API: Calculate travel duration and distance in miles between two locations using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer the below sample. You need to convert the km/mtr to miles like below.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;table&gt; &lt;tr&gt; &lt;td&gt; Origin Address: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblOriginAddress&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Destination Address: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDestinationAddress&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Duration Text: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDurationText&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Duration Value: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDurationValue&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Distance Text: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDistanceText&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Distance Value: &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblDistanceValue&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { string url = &#34;https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&amp;destinations=BR7%205QP&amp;key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg&#34;; 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[&#34;DistanceMatrixResponse&#34;].Rows[0][&#34;origin_address&#34;].ToString(); lblDestinationAddress.Text = dsResult.Tables[&#34;DistanceMatrixResponse&#34;].Rows[0][&#34;destination_address&#34;].ToString(); lblDurationText.Text = dsResult.Tables[&#34;duration&#34;].Rows[0][&#34;text&#34;].ToString(); lblDurationValue.Text = dsResult.Tables[&#34;duration&#34;].Rows[0][&#34;value&#34;].ToString(); if (dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;text&#34;].ToString().Split(&#39; &#39;)[1].ToUpper() == &#34;KM&#34;) { // 1 kilometer is equal to 0.62137119 miles lblDistanceText.Text = Math.Round((Convert.ToDouble(dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;text&#34;].ToString().Split(&#39; &#39;)[0]) * 0.621371), 2).ToString() + &#34; miles&#34;; } else if (dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;text&#34;].ToString().Split(&#39; &#39;)[1].ToUpper() == &#34;M&#34;) { // 1 kilometer is equal to 0.000621371 miles lblDistanceText.Text = (Convert.ToDouble(dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;text&#34;].ToString().Split(&#39; &#39;)[0]) * 0.000621371).ToString() + &#34; miles&#34;; } lblDistanceValue.Text = dsResult.Tables[&#34;distance&#34;].Rows[0][&#34;value&#34;].ToString(); } } }</pre> <p><span style="text-decoration: underline;">VB.Net</span></p> <pre class="brush: vb">Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim url As String = &#34;https://maps.googleapis.com/maps/api/distancematrix/xml?origins=SE9%204QH&amp;destinations=BR7%205QP&amp;key=AIzaSyBojHBt73ZegOGb2Kj8boi0HXMKRyfftVg&#34; 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(&#34;DistanceMatrixResponse&#34;).Rows(0)(&#34;origin_address&#34;).ToString() lblDestinationAddress.Text = dsResult.Tables(&#34;DistanceMatrixResponse&#34;).Rows(0)(&#34;destination_address&#34;).ToString() lblDurationText.Text = dsResult.Tables(&#34;duration&#34;).Rows(0)(&#34;text&#34;).ToString() lblDurationValue.Text = dsResult.Tables(&#34;duration&#34;).Rows(0)(&#34;value&#34;).ToString() If dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;text&#34;).ToString().Split(&#34; &#34;c)(1).ToUpper() = &#34;KM&#34; Then &#39; 1 kilometer is equal to 0.62137119 miles lblDistanceText.Text = Math.Round((Convert.ToDouble(dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;text&#34;).ToString().Split(&#34; &#34;c)(0)) * 0.621371), 2).ToString() + &#34; miles&#34; ElseIf dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;text&#34;).ToString().Split(&#34; &#34;c)(1).ToUpper() = &#34;M&#34; Then &#39; 1 kilometer is equal to 0.000621371 miles lblDistanceText.Text = (Convert.ToDouble(dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;text&#34;).ToString().Split(&#34; &#34;c)(0)) * 0.000621371).ToString() + &#34; miles&#34; End If lblDistanceValue.Text = dsResult.Tables(&#34;distance&#34;).Rows(0)(&#34;value&#34;).ToString() End Using End Using End Sub</pre> <p><span style="text-decoration: underline;">Output</span></p> <table border="1"> <tbody> <tr> <td>Origin Address:</td> <td><span id="lblOriginAddress">Highcombe Cl, London SE9 4QH, UK</span></td> </tr> <tr> <td>Destination Address:</td> <td><span id="lblDestinationAddress">Chislehurst BR7 5QP, UK</span></td> </tr> <tr> <td>Duration Text:</td> <td><span id="lblDurationText">8 mins</span></td> </tr> <tr> <td>Duration Value:</td> <td><span id="lblDurationValue">493</span></td> </tr> <tr> <td>Distance Text:</td> <td><span id="lblDistanceText">2.05 miles</span></td> </tr> <tr> <td>Distance Value:</td> <td><span id="lblDistanceValue">3290</span></td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/208704/Google-API-Calculate-travel-duration-and-distance-in-miles-between-two-locations-using-C-and-VBNet-in-ASPNet/Tue, 03 Oct 2017 00:42:42 GMT