【问题标题】:Google api autocomplete predictions with typeahead.js not working使用 typeahead.js 的 Google api 自动完成预测不起作用
【发布时间】:2017-11-13 11:14:17
【问题描述】:

我想显示来自谷歌自动完成预测的输入提示。

 var service = new google.maps.places.AutocompleteService();


          $('.delivery_areas').typeahead({
                highlight: true,
                minLength: 3,  

            },{

                name: 'predictions',
                limit: 6,
                async: true,
                source: function(q, sync,async) {
                    matches = [];
                    service.getPlacePredictions({
                        input: q
                    }, function(predictions, status) {
                        if (status == google.maps.places.PlacesServiceStatus.OK) {
                            predictions.forEach(function(prediction) {
                                matches.push(prediction.description);
                            });
                        }
                    });
                  //console.log(matches) display a list of suggestions from google
                  async(matches);
                 //cb(matches) also wont work  
                }
            });

不知何故,它不适用于来自自动完成 API 的结果。 我已经尝试过 sync 和 async 回调。

注意:Typeahead 与示例数据源一起正常工作,API 端也没有问题。结果即将到来并存储在匹配数组中。

【问题讨论】:

    标签: javascript google-maps-api-3 typeahead.js


    【解决方案1】:

    回调不在正确的位置

    $('.delivery_areas').typeahead({
                    highlight: true,
                    minLength: 3,  
    
                },{
    
                    name: 'predictions',
                    limit: 6,
                    async: true,
                    source: function(q, sync,async) {
                        matches = [];
                        service.getPlacePredictions({
                            input: q
                        }, function(predictions, status) {
                            if (status == google.maps.places.PlacesServiceStatus.OK) {
                                predictions.forEach(function(prediction) {
                                    matches.push(prediction.description);
                                });
                             async(matches);
                            }
                        });
    
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多