开发者问题收集

react-places-autocomplete 限制

2017-10-10
8576

我刚刚开始使用 react-places-autocomplete 库,我找不到如何将预测限制在指定的国家/地区。

我可以找到谷歌地图自动完成 ComponentRestrictions ,但找不到 react-places-autocomplete

可能吗?

3个回答

//只需使用

 <PlacesAutocomplete
    searchOptions={searchOptions}

//并添加一个 const

const searchOptions = {
  componentRestrictions: { country: ['us'] },
  types: ['city']
}
Alejandro Arias
2019-06-29

您可以使用 option 属性来限制您的结果。

来自文档;

options

Type: Object Required: false Default: {}

You can fine-tune the settings passed to the AutocompleteService class with options prop. This prop accepts an object following the same format as google.maps.places.AutocompletionRequest (except for input, which comes from the value of the input field).

// 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}
/>
bennygenel
2017-10-10

这是执行此操作的方法,来自文档

<GooglePlacesAutocomplete
  autocompletionRequest={{
    componentRestrictions: {
      country: ['us', 'ca'],
    }
  }}
/>
Hugo
2022-11-22