【发布时间】:2018-03-22 00:12:42
【问题描述】:
我刚开始使用react-places-autocomplete 库,但找不到如何将预测限制在指定国家/地区。
我可以在 google maps 自动完成 ComponentRestrictions 中找到它,但在 react-places-autocomplete 库中找不到它
有可能吗?
【问题讨论】:
标签: reactjs google-maps autocomplete
我刚开始使用react-places-autocomplete 库,但找不到如何将预测限制在指定国家/地区。
我可以在 google maps 自动完成 ComponentRestrictions 中找到它,但在 react-places-autocomplete 库中找不到它
有可能吗?
【问题讨论】:
标签: reactjs google-maps autocomplete
//只使用
<PlacesAutocomplete
searchOptions={searchOptions}
//并添加一个常量
const searchOptions = {
componentRestrictions: { country: ['us'] },
types: ['city']
}
【讨论】:
您可以使用 option 属性限制您的结果。
来自文档;
选项
Type: Object Required: false Default: {}您可以微调传递给 AutocompleteService 类的设置 带选项道具。这个道具接受一个对象 格式为
google.maps.places.AutocompletionRequest(输入除外, 来自输入字段的值)。
// these options will bias the autocomplete predictions toward Sydney, Australia with a radius of 2000 meters,
// and limit the results to addresses only
const options = {
location: new google.maps.LatLng(-34, 151),
radius: 2000,
types: ['address']
}
<PlacesAutocomplete
inputProps={inputProps}
options={options}
/>
【讨论】:
google.maps.places.AutocompletionRequest 的componentRestrictions。请检查答案中提供的链接。
{} 的情况下尝试它,所以我认为不是这样。对不起,谢谢!