我刚刚开始使用 react-places-autocomplete 库,我找不到如何将预测限制到指定的国家/地区。
我可以为谷歌地图自动完成ComponentRestrictions找到它,但不能为react-places-autocomplete库找到它
可以吗?
//只需使用
<PlacesAutocomplete
searchOptions={searchOptions}
//并添加一个const
const searchOptions = {
componentRestrictions: { country: ['us'] },
types: ['city']
}
option
属性限制结果。
来自文档;
选项
Type: Object Required: false Default: {}
您可以微调传递给 AutocompleteService 类的设置 与选项道具。这个 prop 接受一个遵循相同的对象 格式为
(输入除外, 来自输入字段的值)。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}
/>
这是文档中的方法
<GooglePlacesAutocomplete
autocompletionRequest={{
componentRestrictions: {
country: ['us', 'ca'],
}
}}
/>
<Autocomplete
options={{
types: ["(regions)"],
componentRestrictions: { country: "ru" },
}}
/>