Javascript Google AutocompleteService getPlacePredictions不工作
我正在尝试在我的站点中启动自定义的自动完成。我尝试使用谷歌的PlacesAutoCompleteAPI来实现这一点。 我希望首先将预测结果输出到日志中,以验证它是否正常工作,但它似乎没有到达回调函数,我也没有收到任何错误消息。没有 我首先添加了Google places库:Javascript Google AutocompleteService getPlacePredictions不工作,javascript,html,google-maps,google-places-api,googleplacesautocomplete,Javascript,Html,Google Maps,Google Places Api,Googleplacesautocomplete,我正在尝试在我的站点中启动自定义的自动完成。我尝试使用谷歌的PlacesAutoCompleteAPI来实现这一点。 我希望首先将预测结果输出到日志中,以验证它是否正常工作,但它似乎没有到达回调函数,我也没有收到任何错误消息。没有 我首先添加了Google places库: <script src="https://maps.googleapis.com/maps/api/js?key=[Here-I-Entered-My-API-Key]&libraries=places">
<script src="https://maps.googleapis.com/maps/api/js?key=[Here-I-Entered-My-API-Key]&libraries=places"></script>
这样做没有任何结果,它没有显示自动完成选项
let searchInput = $( ".search-input" )[0];
autocomplete = new google.maps.places.Autocomplete(searchInput, {
types: ["address"]
});
// Create a new session token.
let sessionToken = new google.maps.places.AutocompleteSessionToken();
// Pass the token to the autocomplete service.
let autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions(
{
input: "Mitt",
type: ["address"],
componentRestriction: {country: 'de'},
sessionToken: sessionToken
},
function (predictions, status) {
console.log("Query resulted in -> " + status);
console.log("Received " + predictions.length + " predictions");
for(let prediction of predictions){
console.log(prediction);
console.log(prediction.description);
}
}
);
谢谢 我已经设法解决了这个问题。 显然,导致这个问题的原因是一次调用GoogleAPI库和一次调用GoogleAPI进行映射回调。 需要将其组合在一起,如下所示:
<script src="https://maps.googleapis.com/maps/api/js?key=[Here-I-Entered-My-API-Key]&libraries=places&callback=initMap"></script>
您应该提交一份支持案例。