没有国家/地区名称的Google自动填充建议

时间:2018-05-30 05:13:23

标签: google-maps-api-3 autocomplete country googleplacesautocomplete google-suggest

我正在寻找获得没有国名的建议的可能性。

我有以下js代码:

var options = {
    types: ['(cities)'],
        componentRestrictions: {country: "us"}
     };
autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')), options);

结果是例如美国德克萨斯州奥斯汀

现在我只想要那个例子

预计结果如下德克萨斯州奥斯汀

查看图片链接后的更多信息 click here

你知道吗?谢谢。 Dhiraj Gangal

1 个答案:

答案 0 :(得分:1)

我对此进行了更多调查,我在这里有更多信息:

使用服务: https://developers.google.com/maps/documentation/javascript/places-autocomplete?hl=de#place_autocomplete_service

AutocompletePrediction定义服务返回的内容https://developers.google.com/maps/documentation/javascript/reference?hl=de#AutocompletePrediction

和这里的PredictionTerm https://developers.google.com/maps/documentation/javascript/reference?hl=de#PredictionTerm是“描述”的一部分,最终会显示结果。

有趣的部分实际上是PredictionTerm,它是整个地方建议的一部分。

如果您查看此示例:https://developers.google.com/maps/documentation/javascript/examples/places-queryprediction

您可以轻松获取建议结果的每个PredictionTerm,如下面的代码段所示:

predictions.forEach(function(prediction) {
     var li = document.createElement('li');
     var city = prediction.terms[2];
     var content = "Results: "+city;
     li.appendChild(document.createTextNode(prediction.description));
     document.getElementById('results').appendChild(li);
});

我做了一个例子:https://jsfiddle.net/yfkpvf72/1/