【问题标题】:Material UI - Google Maps Autocomplete - Restrict to city and state?Material UI - Google Maps Autocomplete - 仅限于城市和州?
【发布时间】:2021-09-09 07:10:41
【问题描述】:

我正在使用 Material UI 附带的 Google Maps Places Autocomplete,但我一直在尝试限制结果。当用户开始输入我想要返回的唯一建议是城市,州供他们选择。

以下是 MUI 文档的链接,其中包含示例: Material UI - Google Places Autocomplete

谢谢!

【问题讨论】:

    标签: reactjs google-maps autocomplete material-ui google-places-api


    【解决方案1】:

    看起来库中的代码正在使用AutocompleteService.getPlacePredictions。要实现您的用例,您需要将值为 ['(cities)']types 属性包含到您的 AutocompleteService.getPlacePredictions 请求中。这将指示 Places 服务返回与 locality 或 Administrative_area_level_3 相匹配的结果,如 here

    所述

    您可以将其添加到代码示例中的 fetch 中,如下所示:

     fetch({ input: inputValue, types: ['(cities)'] }, (results) => {
          if (active) {
            let newOptions = [];
    
            if (value) {
              newOptions = [value];
            }
    
            if (results) {
              newOptions = [...newOptions, ...results];
            }
    
            setOptions(newOptions);
          }
        });
    

    这是sample code。确保使用您的 API 密钥来运行示例。

    【讨论】:

    • 太简单了!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 2018-07-25
    • 1970-01-01
    • 2013-02-11
    • 2019-01-27
    • 2012-11-02
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多