ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Google Places Autocomplete does not work on mobile

Open ch000 opened this issue 12 years ago • 42 comments

It's not possible to select an google places autocomplete entry on mobile. I'm using the ngAutocomplete directive.

It does work on desktop: image

And this happens on mobile: image

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

ch000 avatar Jul 17 '14 14:07 ch000

Hey @ch000, so data-tap-disable would be great here, but since the container used to make the autocomplete is being generated and we have no controller over it, we have to use a directive to work around it.

So try this

.directive('disableTap', function($timeout) {
  return {
    link: function() {

      $timeout(function() {
        document.querySelector('.pac-container').setAttribute('data-tap-disabled', 'true')
      },500);
    }
  };
});

http://plnkr.co/edit/SjSDzb70NcXImWk9n5lX?p=preview

It works now in plunker, just make sure to use it on the body tag. Going to close this for now, but reopen it if the issue is not resolved.

mhartington avatar Jul 22 '14 20:07 mhartington

The issue still stands, when you test the plunker in emulation mode you will see what I am talking about.

Jdruwe avatar Aug 29 '14 19:08 Jdruwe

Yes, the issue still stands

zdivozzo avatar Sep 03 '14 01:09 zdivozzo

+1 still an issue

Fire-Brand avatar Sep 04 '14 16:09 Fire-Brand

anyone found any solution for this problem. i also having same problem.

please help me and contact [email protected]

kaushal2406 avatar Sep 12 '14 07:09 kaushal2406

Sorry for the delay. This is how I got it working:

.directive('disableTap', function($timeout) {
  return {
    link: function() {
      $timeout(function() {
        // Find google places div
        _.findIndex(angular.element(document.querySelectorAll('.pac-container')), function(container) {
          // disable ionic data tab
          container.setAttribute('data-tap-disabled', 'true');
          // leave input field if google-address-entry is selected
          container.onclick = function() {
            document.getElementById('autocomplete').blur();
          };
        });
      },500);
    }
  };
});

I'm using Lo-Dash if more than one .pac-container exists (but you could do the same without Lo-Dash).

http://plnkr.co/edit/1VIuREWDNunaimL4qMAz?p=preview

ch000 avatar Sep 12 '14 15:09 ch000

@ch000 Looks like that with your approach the issue is solved, at least for me. Is wierd though, that only works on a real device. I mean, if you try to emulate a real device with chrome as @Jdruwe commented, the problem persists.

the vanilla/angularjs way to implement your fix in case anyone is interested:

.directive('disabletap', function($timeout) {
  return {
    link: function() {
      $timeout(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('type-selector').blur();
        });

      },500);

    }
  };
});

BondaCB avatar Oct 20 '14 19:10 BondaCB

I'd like to point out this community project

https://github.com/israelidanny/ion-google-place

Seems to be a good solution

mhartington avatar Oct 20 '14 19:10 mhartington

ion-google-place is working really well for me. I needed to make a few simple changes so it could use the Google Places API rather than Google's geocoder, but I've documented my changes here (https://github.com/israelidanny/ion-google-place/issues/3) and will submit a PR in the next few days.

quipu avatar Oct 20 '14 20:10 quipu

I've been spending the day trying to get this to work with no luck. the example above http://plnkr.co/edit/1VIuREWDNunaimL4qMAz?p=preview @ch000 had doesn't work on ipad ios8 or ios 7 on iphone.

I tried your suggestion @gsabater but it only works in the browser emulators. xcode emulator and iphone actual device it won't add the data-tap-disable attribute. haven't gone down the path of the ion-google-place project yet as I'm not sure how to implement it into my map view. And was hoping to get this working. any thoughts?

wallstudios avatar Nov 18 '14 07:11 wallstudios

I would suggest to use $interval instead of $timeout. This way you a more independent of time delays.

steefaan avatar Dec 09 '14 21:12 steefaan

Instead of using custom directive and $timeout, I wrap the same code in a controller function and trigger using ng-focus on the input. Worked perfectly for me.

ph88lee avatar Dec 16 '14 10:12 ph88lee

@kandizzy @php-engineer @gsabater @ch000 both solutions work on Chrome emulator, but persists on iOS 8.1.2 Chrome Version 39.0.2171.50.

I'm trying to understand what the blur() call is intended to do—it's not snapping the autocomplete click event back to the input form.

In the code below, event.toElement.parentElement returns the .pac-item; I'm trying to figure out how to proceed from here.

Should I write a directive that has access to scope and element to bind the input value to the text-field?

function() {
      $timeout(function() {
        var container = document.getElementsByClassName('pac-container');
        var parent = document.getElementById('autocomplete');
        // wrapping disabledtap in parent of input
        // angular.element(parent).wrap('
'); // leave input field if google-address-entry is selected angular.element(container).on("click", function(event){ console.log('on click', event.toElement.parentElement, event, container); // var target = event.target; // return event.target }); angular.element(parent).on('blur', function(){ console.log('inside BLUR') }) console.log('disabletap ready') },5000);

raywu avatar Dec 18 '14 07:12 raywu

@mhartington the ion-google-place directive does not seem to work. However, @gsabater 's slolution only works when you relode content in Safari Developer tools. It seems that the directive does not see the places dynamic div when you navigate to the view, but when you refresh while being on the veiw the directive adds needed class: before refresh: screen shot 2015-02-11 at 3 29 31 pm

after page refresh the directive adds needed class: screen shot 2015-02-11 at 3 29 52 pm

Also i have noticed that if you go back and forward google just appends multiple divs: screen shot 2015-02-11 at 3 26 51 pm

Additi

okonon avatar Feb 11 '15 20:02 okonon

@quipu @mhartington it seems that ion-google-place does not work with current ionic-beta14. What version of ionic you are using ion-google-plase directive with?

okonon avatar Feb 11 '15 20:02 okonon

Did you use it with simple way?.Test it with simple javascript.Here is a link how to use it. http://www.w3docs.com/learn-javascript/places-autocomplete.html

Hazarapet avatar Feb 12 '15 17:02 Hazarapet

Hey guys! Have someone came up with solution for @raywu issue? I'm facing the same one right now..

AleksMeshkov avatar Mar 20 '15 14:03 AleksMeshkov

As @ph88lee said, I just added the relevant code to a controller function that triggered using ng-focus and it worked well like so:

$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('your_input_id').blur();
    });
  }

TillaTheHun0 avatar Apr 06 '15 00:04 TillaTheHun0

@TillaTheHun0 Hey can you paste the exact way you did it? What code did you move into a controller?

jason-engage avatar Apr 23 '15 06:04 jason-engage

@jason-engage I took the relevant code and placed it into a function like above. So in my controller:

.controller('MyCtrl', function($scope) {
  $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('searchBar').blur();
    });
  };
})

I then used the ng-focus directive to execute the expression on the desired element like so:

<input id="searchBar" type="text" placeholder="Search" ng-focus="disableTap()"> 

This basically fires an event when the element gains focus, switches the containers 'data-tap-disabled' attribute to true. When you click the container, it removes focus, thus emulating user interaction. Hope this helps.

TillaTheHun0 avatar Apr 24 '15 14:04 TillaTheHun0