Google Places AutocompleteService按国家/地区过滤 [英] Google Places AutocompleteService filtering by country

查看:198
本文介绍了Google Places AutocompleteService按国家/地区过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个自定义自动填充字段,在其中显示Google地方信息中的位置以及数据库中与搜索查询匹配的事件.因此,我使用Google Places Autocomplete Service来获取查询预测,而不是直接将Places Autocomplete插入我的文本字段中.

I'm setting up a custom autocomplete field where I show locations from Google Places and events from my database that match the search query. For this reason, I'm using the Google Places Autocomplete Service to get the query predictions rather than plugging in the Places Autocomplete directly into my textfield.

问题是我不知道如何使用自动完成"服务按国家/地区过滤地点自动填充"建议.

The problem is I can't figure out how to filter Places Autocomplete suggestions by country using the AutoComplete service.

我尝试过:

var service = new google.maps.places.AutocompleteService(null, {
        types: ['cities'],
        componentRestrictions: {country: "au"}
    });

,但仍显示来自德国和法国的自动填充选项:(

but it still shows autocomplete options from Germany and France:(

有什么建议吗?

推荐答案

调用服务时需要传递限制,而不是在创建服务时传递限制.在这里:

You need to pass the restrictions when you call the service, not when you create it. Here:

//create service
service = new google.maps.places.AutocompleteService();

//perform request. limit results to Australia
var request = {
    input: 'Brisbane',
    componentRestrictions: {country: 'au'},
};
service.getPlacePredictions(request, callback);

这篇关于Google Places AutocompleteService按国家/地区过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆