Click here to Skip to main content
15,887,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Reader,

I am using Google Maps for displaying Source and Destination. But I want to use AutoComplete so that it will ease user to search path easily. I am using built-in functionality of Google Maps for autocomplete. Kindly view the following function:

JavaScript
<script type="text/javascript"> 

function Initialize() {

         var autocomplete = new google.maps.places.Autocomplete('<%=TextBox1.ClientID%>');
         autocomplete.bindTo('bounds', '<%=GMap1.ClientID%>');
     }
 google.maps.event.addDomListener(window,'load',Initialize);

</script>


ASP.NET
<body>
   <form id="form1"  runat="server">
    
                    <asp:TextBox ID="TextBox1" runat="server" Width="120px"></asp:TextBox>
              
                    <cc1:GMap ID="GMap1"  runat="server" Height="240px" Width="570px" enableServerEvents="True"  />
               
   </form>
    
</body>


The AutoComplete in the above code is not working and don't know why. I have tried many google searches but unfortunately none works when I integrate with my code.

Please help me to resolve this problem by pointing out the mistake.

Waiting for your kind reply,
Posted
Updated 7-Apr-16 7:03am

1 solution

HTML
Please try this code:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script type="text/javascript">

    function Initialize() {

        var mapOptions = {
            center: new google.maps.LatLng(-33.8688, 151.2195),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('GMap1'), mapOptions);

        var input = document.getElementById('TextBox1');
        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo('bounds', map);

        google.maps.event.addListener(autocomplete, 'place_changed', function () {

            var place = autocomplete.getPlace();
            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
            }
        });

    }
    google.maps.event.addDomListener(window, 'load', Initialize);

</script>

<input type="text" id="TextBox1">
        <div id="GMap1" style="height: 240px; width:570px" ></div>
 
Share this answer
 
Comments
Abhai Oza 12-Jul-16 8:29am    
I have try this code but not successfully execution.
Merchant Gazi Abbas 6-Jun-19 8:30am    
I try But its does work Please Give a New Solution..
Textbox does not a auto locate

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