Javascript 如何获取在变量中返回的GooglePlaceAutoCompleteAPI的结果,而不是将结果以HTML输入元素的形式提供给客户端
我在angularjs中使用node,希望获得具有Place Autocomplete、Google Places API Web服务的城市Javascript 如何获取在变量中返回的GooglePlaceAutoCompleteAPI的结果,而不是将结果以HTML输入元素的形式提供给客户端,javascript,node.js,google-maps,google-maps-api-3,Javascript,Node.js,Google Maps,Google Maps Api 3,我在angularjs中使用node,希望获得具有Place Autocomplete、Google Places API Web服务的城市 var autocomplete = new google.maps.places.Autocomplete(yourHTMLElement,{types: ['(cities)']}); 问题是,我不想得到结果,因为用户在输入文本中键入,如上面的示例所示,但我想将其放入一个变量中,并首先对结果进行一些更改。为此,您需要查看。使用Autocomple
var autocomplete = new google.maps.places.Autocomplete(yourHTMLElement,{types: ['(cities)']});
问题是,我不想得到结果,因为用户在输入文本中键入,如上面的示例所示,但我想将其放入一个变量中,并首先对结果进行一些更改。为此,您需要查看。使用AutocompleteService
类以编程方式获取预测。AutocompleteService
在数组对象中获取预测,但它不会在映射本身中添加任何UI控件
您可以创建AutocompleteService对象以编程方式检索预测。调用getPlacePredictions()检索匹配的位置,或调用getQueryPredictions()检索匹配的位置和建议的搜索词。注意:AutocompleteService不添加任何UI控件。相反,上述方法返回一个预测对象数组。每个预测对象都包含预测文本,以及参考信息和结果与用户输入匹配的详细信息
另外,请参阅本节,了解您的具体案例
这是一个带有注释的功能,将帮助您实现该结果:
function initService() {
const displaySuggestions = (predictions, status) => {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
// use forEch method to populate the
// returned preditions in the Array
predictions.forEach(prediction => {
// here you can use an NG directive in your Angular app
let li = document.createElement('li');
li.appendChild(document.createTextNode(prediction.description));
document.getElementById('results').appendChild(li);
});
};
// change the string inside the input property as you wish and get the predicted list stored in a variable
const service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({
input: 'pizza near roma'
}, displaySuggestions);
}
你需要调查这件事的原因。使用AutocompleteService
类以编程方式获取预测。AutocompleteService
在数组对象中获取预测,但它不会在映射本身中添加任何UI控件
您可以创建AutocompleteService对象以编程方式检索预测。调用getPlacePredictions()检索匹配的位置,或调用getQueryPredictions()检索匹配的位置和建议的搜索词。注意:AutocompleteService不添加任何UI控件。相反,上述方法返回一个预测对象数组。每个预测对象都包含预测文本,以及参考信息和结果与用户输入匹配的详细信息
另外,请参阅本节,了解您的具体案例
这是一个带有注释的功能,将帮助您实现该结果:
function initService() {
const displaySuggestions = (predictions, status) => {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
// use forEch method to populate the
// returned preditions in the Array
predictions.forEach(prediction => {
// here you can use an NG directive in your Angular app
let li = document.createElement('li');
li.appendChild(document.createTextNode(prediction.description));
document.getElementById('results').appendChild(li);
});
};
// change the string inside the input property as you wish and get the predicted list stored in a variable
const service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({
input: 'pizza near roma'
}, displaySuggestions);
}