2017-11-13 7 views
-1

Google自動補完予測の先取り候補を表示したい。Googleのapiでtypeahead.jsの予測が自動で機能しない

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の終了に問題はありません。結果は来て、マッチ配列に格納されます。

答えて

0

コールバックは、適切な場所ではありませんでした

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

       } 
      });