【问题标题】:Force user to use only place autocomplete results强制用户仅使用地点自动完成结果
【发布时间】:2020-07-02 17:02:10
【问题描述】:

我正在使用“Places”Google API 来使地址输入字段自动完成。

网站提供的服务仅限于某个区域,因此我限制了自动完成过滤器。唯一的问题是用户不必强制使用谷歌过滤器,而是可以输入任何内容。

如何强制用户从自动完成过滤器中选择某些内容?

这是我的codeJsFiddle

【问题讨论】:

    标签: javascript html google-maps google-maps-api-3 google-places-api


    【解决方案1】:

    如果您只需要在用户未从“地点自动完成”列表中选择地点时什么都不做,那么您只需这样做:

      autocomplete.addListener('place_changed', function() {
        var place = autocomplete.getPlace();
        if (!place.geometry) {
          // User entered the name of a Place that was not suggested and
          // pressed the Enter key, or the Place Details request failed.
          return;
        }
    
        // If the place has a geometry, then present it on a map.
        if (place.geometry.viewport) {
          map.fitBounds(place.geometry.viewport);
        } else {
          map.setCenter(place.geometry.location);
          map.setZoom(17);
        }
      });
    

    这是基于 Google 的examplejsfiddle

    希望这会有所帮助!

    【讨论】:

    • 感谢 Evan,但遗憾的是它不起作用...不会调用事件侦听器。并且条件不可检查
    • 没关系,我也有自己的工作 API 密钥,所以我应该能够很好地测试你的应用程序。 :)
    • 我在我的问题中添加了 JsFiddle。感谢您的帮助:)
    • 完美,我根据我的代码调整了您的解决方案,效果很好!非常感谢您的帮助和耐心:)
    • 太棒了!很高兴能得到帮助:)
    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多