Implementing Google Place AutoComplete Textbox by Finding Road Distance Between Source And Destination

Here in this article I am going to explain how to use Google place AutoComplete textbox and find the accurate distance between the source and destination place using ASP.NETAutoComplete is a feature of the Places library in the Google Maps JavaScript API. You can use autocomplete to give your application to search words on the basis of characters entered by you. When a user starts typing an address, autocomplete will fill in the rest.

The  following JavaScript is used to implement autocomplete location search textbox in your webpage. So to use this you have to use this javascript in your project.
  1. http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places  
So I have the following design where I have two textboxes, one button and one label. There are two textboxes where I want to attach Google place autocomlete search and finally when I click the button it should display the exact distance between these two places and bind in the label to show me. So here is my designing form.

 
And here is the html code for the design.
  1. <body>  
  2.   
  3. <table border="0" cellpadding="0" cellspacing="3" style="border:solid; background-color:aqua;">  
  4.         <tr>  
  5.             <td>  
  6.   
  7.               <b>Source:</b>   
  8.                 </td>   
  9.             </tr>  
  10.             <tr>  
  11.                 <td>  
  12.                       <input type="text" id="txt_Source"  style="width: 500px;height:30px" />  
  13.                 </td>  
  14.             </tr>  
  15.         <tr>  
  16.                 <td>  
  17.                        <b>Destination:</b>  
  18.                 </td>  
  19.             </tr>  
  20.          <tr>  
  21.                 <td>  
  22.                       <input type="text" id="txt_Destination"  style="width: 500px;height:30px" />  
  23.                 </td>  
  24.             </tr>  
  25.         <tr>  
  26.             <td>  
  27.                  <input type="button" value="Get Distance" onclick="getdistance()" />  
  28.             </td>  
  29.         </tr>  
  30.         <tr>  
  31.             <td>  
  32.                   <asp:Label runat="server" ID="lbl_distance"></asp:Label>  
  33.             </td>  
  34.         </tr>  
  35.              
  36.     </table>  
  37.      
  38. </body>  
For implementing auto location search use the following JavaScript for both search and destination. 
  1.  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>  
  2.     <script type="text/javascript">  
  3.         var source, destination;  
  4.          
  5.         var directionsService = new google.maps.DirectionsService();  
  6.         google.maps.event.addDomListener(window, 'load'function () {  
  7.             new google.maps.places.SearchBox(document.getElementById('txt_Source'));  
  8.             new google.maps.places.SearchBox(document.getElementById('txt_Destination'));  
  9.           
  10.               
  11.         });  
  12. </script>  
Now if we check  source textbox it will be applied with the Google location Textbox as follows.

 
Now, let's check the second one, that is Destination Textbox:.

 
Now our second work is to calculate the distance between the source and destination  entered in the textbox, so these are the places I want distance.

 
 Now implement this JavaScript and call this on button click.
  1. function getdistance() {  
  2.            source = document.getElementById("txt_Source").value;  
  3.            destination = document.getElementById("txt_Destination").value;  
  4.            var service = new google.maps.DistanceMatrixService();  
  5.            service.getDistanceMatrix({  
  6.                origins: [source],  
  7.                destinations: [destination],  
  8.                travelMode: google.maps.TravelMode.DRIVING,  
  9.                unitSystem: google.maps.UnitSystem.METRIC,  
  10.                avoidHighways: false,  
  11.                avoidTolls: false  
  12.            }, function (response, status) {  
  13.                if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {  
  14.                    var distance = response.rows[0].elements[0].distance.text;  
  15.                   // var duration = response.rows[0].elements[0].duration.text;  
  16.                    var lbl_distance = "Distance: " + distance;  
  17.                    document.getElementById('lbl_distance').innerHTML = lbl_distance;  
  18.                     
  19.   
  20.        }  
  21.        else {  
  22.                   alert("Unable to calculate distance.");  
  23.                }  
  24.            });  
  25.        }  
  26.         
Now call this on button click. It will show the result as below.

 
Now we are getting the distance.

So in this way we will find the distance. Here I have posted the whole code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Distance.aspx.cs" Inherits="Distance" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <head>  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         body  
  9.         {  
  10.             font-family: Arial;  
  11.             font-size: 10pt;  
  12.         }  
  13.     </style>  
  14. </head>  
  15. <body>  
  16.     <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>  
  17.     <script type="text/javascript">  
  18.         var source, destination;  
  19.          
  20.         var directionsService = new google.maps.DirectionsService();  
  21.         google.maps.event.addDomListener(window, 'load'function () {  
  22.             new google.maps.places.SearchBox(document.getElementById('txt_Source'));  
  23.             new google.maps.places.SearchBox(document.getElementById('txt_Destination'));  
  24.           
  25.               
  26.         });  
  27.   
  28.         function getdistance() {  
  29.             source = document.getElementById("txt_Source").value;  
  30.             destination = document.getElementById("txt_Destination").value;  
  31.             var service = new google.maps.DistanceMatrixService();  
  32.             service.getDistanceMatrix({  
  33.                 origins: [source],  
  34.                 destinations: [destination],  
  35.                 travelMode: google.maps.TravelMode.DRIVING,  
  36.                 unitSystem: google.maps.UnitSystem.METRIC,  
  37.                 avoidHighways: false,  
  38.                 avoidTolls: false  
  39.             }, function (response, status) {  
  40.                 if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {  
  41.                     var distance = response.rows[0].elements[0].distance.text;  
  42.                    // var duration = response.rows[0].elements[0].duration.text;  
  43.                     var lbl_distance = "Distance: " + distance;  
  44.                     document.getElementById('lbl_distance').innerHTML = lbl_distance;  
  45.                      
  46.   
  47.         }  
  48.         else {  
  49.                    alert("Unable to calculate distance.");  
  50.                 }  
  51.             });  
  52.         }  
  53.          
  54.     </script>  
  55.     <table border="0" cellpadding="0" cellspacing="3" style="border:solid; background-color:aqua;">  
  56.         <tr>  
  57.             <td>  
  58.   
  59.               <b>Source:</b>   
  60.                 </td>   
  61.             </tr>  
  62.             <tr>  
  63.                 <td>  
  64.                       <input type="text" id="txt_Source"  style="width: 500px;height:30px" />  
  65.                 </td>  
  66.             </tr>  
  67.         <tr>  
  68.                 <td>  
  69.                        <b>Destination:</b>  
  70.                 </td>  
  71.             </tr>  
  72.          <tr>  
  73.                 <td>  
  74.                       <input type="text" id="txt_Destination"  style="width: 500px;height:30px" />  
  75.                 </td>  
  76.             </tr>  
  77.         <tr>  
  78.             <td>  
  79.                  <input type="button" value="Get Distance" onclick="getdistance()" />  
  80.             </td>  
  81.         </tr>  
  82.         <tr>  
  83.             <td>  
  84.                   <asp:Label runat="server" ID="lbl_distance"></asp:Label>  
  85.             </td>  
  86.         </tr>  
  87.              
  88.     </table>  
  89.      
  90. </body>  
  91. </html>  
Thus  I  discussed how to implement the Google Location AutoComplete search and find the distance between two locations.
 
Read more articles on ASP.NET:


Similar Articles