强制用户仅使用地方自动完成结果

问题描述 投票:1回答:1

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

网站提供的服务仅限于特定区域,因此我限制了自动完成过滤器。唯一的问题是用户不会被迫使用Google过滤器,而是可以输入任何内容。

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

javascript html google-maps google-maps-api-3 google-places-api
1个回答
0
投票

如果用户没有从“地点自动完成”列表中选择地点,则只需要做什么,那么您可以简单地做到这一点:

  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 jsfiddleexample

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.