Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am using google maps in my asp.net website, i have two textboxes for find route between them. Everything working fine but i want autocomplete in my two textboxes, while typing it should give hints by google map. Hope you understand my need.

JavaScript
        var lat = '<%=lat%>';
        var lon = '<%=lon%>';
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();

         function InitializeMap() {
                         
             directionsDisplay = new google.maps.DirectionsRenderer();
             var latlng = new google.maps.LatLng(lat, lon);
             var myOptions =
             {
                 zoom: 13,
                 center: latlng,
                 mapTypeId: google.maps.MapTypeId.ROADMAP
             };
             var map = new google.maps.Map(document.getElementById("Gmap"), myOptions);

             directionsDisplay.setMap(map);
             directionsDisplay.setPanel(document.getElementById('directionpanel'));

             var control = document.getElementById('control');
             control.style.display = 'block';

function calcRoute() {

             var start = document.getElementById('from').value;
             var end = document.getElementById('to').value;
             var request = {
                 origin: start,
                 destination: end,
                 travelMode: google.maps.DirectionsTravelMode.DRIVING
             };
             directionsService.route(request, function (response, status) {
                 if (status == google.maps.DirectionsStatus.OK) {
                     directionsDisplay.setDirections(response);
                 }
             });
         }
         
         function btnDirections_onclick() {
             calcRoute();
         }

         window.onload = InitializeMap;

HTML
<table id ="control">
                    <tr>
                    <td>
                        
                    <table>
                    <tr>
                    <td>From:</td>
                    <td><input type="text" id="from" name="from" style="width: 300px" />
                    </td>
                    </tr>

                    <tr>
                    <td>To:</td>
                    <td><input type="text" id="to" name="to" style="width: 300px" /></td>
                    </tr>

                    <tr>
                    <td></td>
                    <td><input id="btnDirections" type="button" value="GetDirections" onclick="return btnDirections_onclick()" /></td>
                    </tr>
                    </table>

                    </td>
                    </tr>
                </table>
                <div id ="Gmap" style="height: 300px; width: 600px; removed 25px; removed 25px;" ></div>
                <br/>
                <div id ="directionpanel"  style="height: 300px; overflow: auto; removed 25px; removed 25px;" ></div>
Posted
Comments
ZurdoDev 1-May-14 9:33am    
I understand your need but do not understand where you are stuck.
Raj Negi 1-May-14 14:17pm    
i am not stuck...i just want to apply the autocomplete functionality to my two textboxes and i dont know how to start.

hey raj i know what you want,for auto fill you use ajax,j-query etc. but you have first data for this. so that you pick data when you type any word, they pick all data related to that word..

for reference you check this link....

http://jqueryui.com/autocomplete/[^]
 
Share this answer
 
JavaScript
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

and add this for autocomplete:
             //autocomplete textbox 'from'
             var fromText = document.getElementById('from');
             var fromAuto = new google.maps.places.Autocomplete(fromText);
             fromAuto.bindTo('bounds', map);
             //autocomplete textbox 'to'
             var toText = document.getElementById('to');
             var toAuto = new google.maps.places.Autocomplete(toText);
             toAuto.bindTo('bounds', map);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900