Google Places API - 按国家/城市/州限制getQueryPredictions?

时间:2012-11-29 06:49:28

标签: google-maps google-places-api

以下是google

提供的getQueryPredictions示例
service.getQueryPredictions({input: 'pizza near'}, callback);

有没有办法限制特定国家/城市/州的结果?

其他功能/组件有能力执行此操作

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

2 个答案:

答案 0 :(得分:24)

使用这个类似的函数(它比getQueryPredictions更强大):

getPlacePredictions(
            {
            input: "pizza near",
            types: ['(cities)'],
            componentRestrictions: {country: 'fr'}
            }, 
            callback);

支持四种类型:企业的“建立”,地址的“地理编码”,行政区域的“(区域)”和地区的“(城市)”。

或者,如果你想使用geyQueryPredictions(),你可以做以下技巧(但这不是一个好方法):

{输入:'披萨靠近'+',AR-M'}

其中'AR-M'是阿根廷的邮政编码,MENDOZA。 (我居住的地方)只需查看您所在位置的邮政编码。

当您显示预测时,请执行以下操作:

for (var i = 0, max = predictions.length; i < max; i++) {
    var address = predictions[i].description.replace(/AR-M,/g, '');
        ...
    }

希望有所帮助。

答案 1 :(得分:1)

目前不支持此功能,对于支持的请求参数,请参阅reference documentation

如果您认为这是一项有用的功能,请添加Places API - Feature Request

相关问题