Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places Autocomplete with Jquery Mobile not working on mobile/touch device

As title suggests I am building a mobile website with JQuery Mobile (1.3.0) and am trying to implement Google Places Autocomplete (API v3) to aid user input of location data.

The autocomplete functions correctly on desktop device, but not when used on a mobile device (I have only tested on iOS 6).

When used on mobile device the dropdown list of relevant locations do appear, but simply disappear when you press one without loading the selection on the map.

I have looked around and seen some solutions that sight the z-index of

.pac-container

as the culprit (see: http://osdir.com/ml/google-maps-js-api-v3/2012-01/msg00823.html).

I have implemented these fixes but to no avail, and I am not convinced that z-index is the problem because I can see that the selected item does change to it's :hover state/colour when pressed on mobile.

Please if anyone has suggestions I am all ears, need any more details let me know.

like image 629
danwild Avatar asked Jun 18 '13 03:06

danwild


People also ask

How Use Google places autocomplete Android?

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.

Is Google places API free for Android?

Places API is not free, however, once you set up your billing account, you will be entitled for a one time $300 free credit(usable for Google Cloud Platform products) and a monthly recurring $200 free credit(exclusive for Google Maps Platform products), after consuming the credits, you will receive an OVER_QUERY_LIMIT ...

Is Google address autocomplete API free?

Autocomplete without Place Details – Per Session (price starting at 0.017 USD per session)


2 Answers

Saravanan's answer is a bit overkill. To fix the conflict with FastClick and PAC, add the needsclick class to both the pac-item and all its children.

$(document).on({
    'DOMNodeInserted': function() {
        $('.pac-item, .pac-item span', this).addClass('needsclick');
    }
}, '.pac-container');
like image 159
Space Devin Avatar answered Nov 08 '22 07:11

Space Devin


Thanks Daniel. But the solution I have given has some performance impact.

I have modifed the FastClick library little bit to accomplish that.

First I have added a param to FastClick constructor, where defaultElCls will be the elements which should not implement fastclick.

function FastClick(layer, defaultElCls) {
    'use strict';
    var oldOnClick, self = this;

    this.defaultElCls = defaultElCls;

Then modify needsClick method:

FastClick.prototype.needsClick = function(target) {
'use strict';
var nodeName = target.nodeName.toLowerCase();

if (nodeName === 'button' || nodeName === 'input') {

    // File inputs need real clicks on iOS 6 due to a browser bug (issue #68)
    // Don't send a synthetic click to disabled inputs (issue #62)
    if ((this.deviceIsIOS && target.type === 'file') || target.disabled) {
        return true;
    }       
} else if (nodeName === 'label' || nodeName === 'video') {
    return true;
}

return ((/\bneedsclick\b/).test(target.className) || (new RegExp(this.defaultElCls).test(target.className)));

};

Then pass pac-item to the FastClick constructor

new FastClick(document.body, "pac-item");

Hope this will be taken care by FastClick library as well :)

like image 40
Saravanan S Avatar answered Nov 08 '22 08:11

Saravanan S