【问题标题】:react-places-autocomplete restrictionsreact-places-autocomplete 限制
【发布时间】:2018-03-22 00:12:42
【问题描述】:

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

我可以在 google maps 自动完成 ComponentRestrictions 中找到它,但在 react-places-autocomplete 库中找不到它

有可能吗?

【问题讨论】:

    标签: reactjs google-maps autocomplete


    【解决方案1】:

    //只使用

     <PlacesAutocomplete
        searchOptions={searchOptions}
    

    //并添加一个常量

    const searchOptions = {
      componentRestrictions: { country: ['us'] },
      types: ['city']
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 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}
      />
      

      【讨论】:

      • 我看到了,这不是我需要的。我的意思是设置该选项不会将预测限制在指定的国家/地区,它只会限制在特定坐标的半径范围内。
      • 这就是你要找的。 option 属性可以包含来自google.maps.places.AutocompletionRequestcomponentRestrictions。请检查答案中提供的链接。
      • 哦,我现在明白了。我在不使用{} 的情况下尝试它,所以我认为不是这样。对不起,谢谢!
      • 没问题。很高兴它有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2017-08-05
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多