Google Places Autocomplete does not work on mobile
It's not possible to select an google places autocomplete entry on mobile. I'm using the ngAutocomplete directive.
It does work on desktop:

And this happens on mobile:

Here an example plunker: http://plnkr.co/edit/UNQCivHSRMEofAP944RJ?p=preview
You can test this behavior on mobile and also with the chrome dev tools device emulation (Version 37.0.2062.3 dev).
Here a corresponding forum thread: http://forum.ionicframework.com/t/google-maps-address-autocomplete-inside-slide-box/5278/8
@TillaTheHun0 Thanks a lot, only this method worked for me
I'm sorry, where is the pac-container in the HTML. I basically have a HTML like this and this code does not work -
<ion-content scroll="false">
<div class="pac-container">
<input type="text" id="searchBar" ng-focus="disableTap()">
</div>
<div id="map-canvas"></div>
</ion-content>
This method does not work.
@sidchilling pac-container is the class used to create the element for the dropdown when using Google Maps Autocomplete control. Since it is generated dynamically via the autocomplete element you setup, you don't specify it anywhere.
It's because that element is generated dynamically that this thread exists, b/c we have no control over its attributes when our views are initially rendered and when our controllers are setup in our app.
your html above should look something like this:
<ion-content scroll="false">
<input type="text" id="searchBar" ng-focus="disableTap()">
<div id="map-canvas"></div>
</ion-content>
then in your controller you would have this:
var input = document.getElementById('searchBar');
var options = { //options for autocomplete object
types: ['geocode']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
you can then have your css to make your pac-container look pretty. (copy pasta from maps API):
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#pac-input:focus {
border-color: #4d90fe;
margin-left: -1px;
padding-left: 14px; /* Regular padding-left + 1. */
width: 401px;
}
.pac-container {
font-family: Roboto;
}
Hope this helps.
@TillaTheHun0 Thanks for your reply. Yes, I figured that out.
This still doesn't work for me. I get the autocomplete options but I can't click on one to select it. When I try the map which is behind gets clicked instead and the autocomplete suggestions goes away. Nothing in the search box and I don't get the places_changed event.
Here is my code -
HTML -
<ion-view view-title="Select a Hotel" cache-view="false" can-swipe-back="false">
<ion-nav-bar class="bar-positive">
<h1 class="title">Select a Hotel</h1>
<ion-nav-buttons side="right">
<button class="button button-icon icon ion-close-round" ng-click="exit()"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-content scroll="false">
<div id="searchBox">
<input id="search-input" type="text" style="height: 50px; width: 100%; padding-left: 10px; padding-right: 10px;" placeholder="Search a Hotel" ng-focus="disableTap()">
</div>
<div class="container" style="height: 100%; width: 100%;">
<div id="map-canvas"></div>
</div>
</ion-content>
</ion-view>
Javascript (generated from coffee script) - [Partial]
$scope.disableTap = function() {
var container;
container = document.getElementsByClassName('pac-container');
angular.element(container).attr('data-tap-disabled', 'true');
return angular.element(container).on('click', function() {
return document.getElementById('search-input').blur();
});
};
$scope.initSearchBox = function() {
var autocomplete, bookingType, input, searchBoxDiv;
bookingType = window.getBookingType();
if (bookingType === 'search') {
input = document.getElementById('search-input');
autocomplete = new google.maps.places.Autocomplete(input);
return google.maps.event.addListener(autocomplete, 'place_changed', function() {
var location, place;
alert("Coming inside place changed");
place = autocomplete.getPlace();
if (place.geometry) {
location = {
latLng: {
lat: place.geometry.location.lat(),
lng: place.geometry.location.lng()
}
};
return $scope.locationSuccess(location);
}
});
} else if (bookingType === 'nearby') {
searchBoxDiv = document.getElementById('searchBox');
return searchBoxDiv.style.display = 'none';
}
};
$scope.showMap = function() {
var divEl;
divEl = document.getElementById('map-canvas');
$scope.c.map = plugin.google.maps.Map.getMap(divEl);
return $scope.c.map.addEventListener(plugin.google.maps.event.MAP_READY, $scope.onMapReady);
};
Do you see anything that I am missing? I am trying to get this to work for hours now with no luck :(
@sidchilling have you tried setting the data-tap-disabled attribute to true for your map div?
@TillaTheHun0 you the man! Thank you :)
@ph88lee Thanks mate, that worked.
i wanted to add backdrop on ng-focus and release on ng-blur but i dont want backdrop on input element how can i do it , what z-index should i set
So anyone has a solution for this at this point? Meaning one that can work on mobile instead of just desktop. Also use Google Places instead of Geocoder.
@quanghoc https://github.com/driftyco/ionic/issues/1798#issuecomment-95943955 this method worked for me, its already present in the comments above
Never mind, I missed this post TillaTheHun0 commented on Apr 5
I've been trying to solve this issue by following the suggestions on the tread, @TillaTheHun0 the pac-container doesn't exist when the ng-focus event fires, any suggestions?
If pac-container doesn't exist, your Google Places API didn't get init in the first place. It should show the suggestion list first.
@alanquigley the pac-container should be generated by the Google Places library when the user starts to type in a place. If it is not present, then check to see if you have Places implemented.
https://developers.google.com/maps/documentation/javascript/places
The idea is to wait for the generated .pac-container to become available then attach the data-tap-disabled attribute to it as well as closing the selection dropdown after the user has tapped a choice.
Setting a fixed timeout is unreliable because your timeout may expire before .pac-container is generated. Setting repeated timeout or interval kept crashing my app on android. I finally found a solution that can detect node insertion using CSS keyframe animation. It's been working well for me on Android. If you try this on iOS, please tell me if it works.
//
// CSS - define a keyframe animation called 'nodeInserted'
//
@keyframes nodeInserted {
from { opacity: 0.99; }
to { opacity: 1; }
}
div.pac-container {
animation-duration: 0.001s;
animation-name: nodeInserted;
}
//
// Jquery - listen for 'nodeInserted' - note the 'one' event handler to avoid redoing the same thing
//
$(document).one('animationstart', 'div.pac-container', function(event){
if(event.originalEvent.animationName === 'nodeInserted'){
$(event.target).attr('data-tap-disabled', true).click(function(){
$('#myAutocompleteInputElem').blur(); //this closes the dropdown
});
}
});
I've also created a gist
It still doesn't work for me on Iphone only. The dropdown for selection is closed when I chose one of the suggestions but then the places_changed event is not triggered. Has anyone found a solution to this?
@mohsinali1317 sounds like data-tap-disabled isn't being set to true on the pac-container, which is that dropdown. Have you tried my suggestion above?
@TillaTheHun0 I found that the issue is with foundations frameworks fastclick JS. It conflicts with the auto complete on touch devices.
TillaTheHun0 solution is working perfectly but while using a modal view it does indeed add data-tap-disabled true to pac-container but the options are still unclickable.
dash.html :
<ion-view view-title="Dashboard">
<ion-content class="padding">
<div ng-click="openModal()">Open</div>
</ion-content>
</ion-view>
<script id="templates/localisation-modal.html" type="text/ng-template">
<ion-modal-view>
<ion-header-bar class="bar-calm">
<h1 class="title">Modal</h1>
<button class="button button-clear button-primary" ng-click="closeModal()">Close</button>
</ion-header-bar>
<ion-content>
<div class="padding center">
<label class="item item-input">
<input ng-model="new_user.address" type="text" placeholder="address" id="autocomplete" ng-autocomplete ng-focus="disableTap()"/>
</label>
</div>
</ion-content>
</ion-modal-view>
</script>
controller.js :
$ionicModal.fromTemplateUrl('templates/localisation-modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal){
$scope.modal = modal
});
// Fixe for ngAutocomplete
$scope.disableTap = function(){
container = document.getElementsByClassName('pac-container');
// disable ionic data tab
angular.element(container).attr('data-tap-disabled', 'true');
// leave input field if google-address-entry is selected
angular.element(container).on("click", function(){
document.getElementById('autocomplete').blur();
});
}