Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google.maps.places and its Autocomplete method are undefined

I'm loading the Google Maps API, jQuery and this Geocomplete plugin. Please note that I'm also specifying the libraries (libraries=places) and eventually my API key:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script src='http://maps.googleapis.com/maps/api/js?v=3&sensor=false&amp;libraries=places&key=xxx'></script>
<script src='js/vendor/jquery.geocomplete.js'></script>

I'm triggering the Geocomplete plugin on the page load

$(window).load(function () {
    $('#my-input').geocomplete();
});

but I always get an error:

Uncaught TypeError: Cannot read property 'Autocomplete' of undefined [jquery.geocomplete.js:153].

Inside the plugin...

this.autocomplete = new google.maps.places.Autocomplete(
 this.input, options
);

I tried to google.maps and it returns a regular object, but places is undefined!

like image 292
Luke Avatar asked Sep 19 '13 16:09

Luke


People also ask

How do I add autocomplete to Google?

From the control panel, select the search engine you want to edit. Click Search features from the menu on the left and then click the Autocomplete tab. Click on the slider to set Enable autocomplete to On. It can take up to 2-4 days for autocompletions tailored to your search engine to start appearing.

How do I create an autocomplete search box in Google Maps?

Google Map with Autocomplete Search BoxCreate an input element ( searchInput ) where the autocomplete addresses search will be added. Create a DIV element ( map ) to display the Google Map. Specify the elements to display the geolocation data.

How do I restrict Google Maps autocomplete to certain cities?

It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.


1 Answers

I tried to google.maps and it returns a regular object, but places is undefined!

That means the google.maps.places library is not loading (the line of code that you posted, shown below is either not correct or is not actually on your page):

<script src='http://maps.googleapis.com/maps/api/js?v=3&sensor=false&amp;libraries=places&key=xxx'></script> 

This is the example from the documentation:

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

The only obvious difference is the &amp; in yours, that should work, but you should probably use it consistently.

TL;DR

Use &libraries not &amp;libraries and it will work.

like image 81
geocodezip Avatar answered Sep 20 '22 01:09

geocodezip