Javascript 使用typeahead.js的Google api自动完成预测不起作用
我想显示来自谷歌自动完成预测的提前输入建议Javascript 使用typeahead.js的Google api自动完成预测不起作用,javascript,google-maps-api-3,typeahead.js,Javascript,Google Maps Api 3,Typeahead.js,我想显示来自谷歌自动完成预测的提前输入建议 var service = new google.maps.places.AutocompleteService(); $('.delivery_areas').typeahead({ highlight: true, minLength: 3, },{ name: 'predictions',
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的结果不起作用。
我尝试了同步和异步回调
注意:Typeahead在示例数据源中工作正常,API端也没有问题。结果即将到来,并存储在matches数组中。回调不在正确的位置
$('.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);
}
});
}
});