I've been working on this for hours trying to figure out why the supposedly simple autocomplete wasn't showing up.
It turns out that in my code, the input element is being set to autocompete="off"
, and the style on the pac-container is display: none
.
I can change these values in DevTools, and it works fine, but I can't figure out how or why these are being set to these values.
My autocomplete is set-up in an Angular Directive, like this, where loadGmaps gets the google api.
template: '<input id="google_places_ac" name="google_places_ac" type="text" class="input-block-level"/>', link: function($scope, elm, attrs){ loadGmaps().then(function(){ var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {}); console.log(autocomplete); google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); $scope.position = { lat: place.geometry.location.lat(), lon: place.geometry.location.lng() }; $scope.$apply(); }); });
---------------------Update----------------------------------------------
Hopefully nobody else is lead astray by this, the autocomplete="off"
is a bit misleading. Even with autocomplete="off"
, the autocomplete still works, so that wasn't the problem. I am over-writing the css of the .pac-container
element, which holds the results with
.pac-container { //this is a fix for google autocomplete not showing up z-index: 10000 !important; display: block !important; }
The problem with this is that once an item has been selected from the autocomplete, the autocomplete remains visible, I'm pretty sure there is a better way to be using autocomplete.
Go to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Places API. If you see the API in the list, you're all set.
The autocomplete request is available at no charge, and the subsequent Place Details call gets charged based on regular Place Details pricing. A Place Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request.
The autocomplete widget is a search dialog with built-in autocomplete functionality. As a user enters search terms, the widget presents a list of predicted places to choose from. When the user makes a selection, a Place instance is returned, which your app can then use to get details about the selected place.
For anybody else who may be stuck or having difficulty with this, ignore the 'autocomplete="no"' value, and don't use 'display: block' to show the '.pac-container'.
go into your chrome devtools and make sure you can see the .pac-container
div. set the z-index of that div in your css. When there is a searched value, google will change the display to block and handle the showing and hiding, you just have to worry about the z-index.
check version of map in index file
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.26&key=YOUR API KEY"></script>
Specify v=3.26 when loading the Google Map API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With