2017-11-13 2 views
-1

Ich möchte Typahead Vorschläge aus Google Autocomplete Vorhersagen anzeigen.Google API Autocomplete Vorhersagen mit typeahead.js nicht funktioniert

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 
       } 
      }); 

Irgendwie funktioniert es nicht für Ergebnisse von Autovervollständigen-API kommen. Ich habe beide sync und asynchrone Rückrufe versucht.

Hinweis: Typeahead funktioniert ordnungsgemäß mit Beispieldatenquellen und es gibt auch kein Problem auf der API-Seite. Die Ergebnisse kommen und werden im Match-Array gespeichert.

Antwort

0

Rückruf war nicht am richtigen Ort

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

       } 
      }); 
Verwandte Themen