0

I use the following code for my website but it does not work. What is the problem? I've included the snippet for error reporting.

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

    <script type="text/javascript">

        function initialize() {
            var options = {
                types: ['(cities)'],
                componentRestrictions: {country: "us"}
            };

            var input = document.getElementById('searchTextField');
            var autocomplete = new google.maps.places.Autocomplete(input , options);
        }

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

    </script>

</head>
<body>

    <div class = "row">
        <input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on">
    </div>
    </body>
Omar
  • 180
  • 1
  • 15
Tanwir Alam
  • 87
  • 3
  • 14

1 Answers1

2

Remove sensor=false&amp; from your script's src attribute and instead add your API key:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>

The following worked for me:

<body>
  <input id="searchTextField" />
  <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
  <script>
    function initialize() {
      var options = {
        types: ['(cities)'],
        componentRestrictions: {
          country: "us"
        }
      };

      var input = document.getElementById('searchTextField');
      var autocomplete = new google.maps.places.Autocomplete(input, options);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
  </script>
</body>
Iavor
  • 1,997
  • 16
  • 27