我正在使用“ 地方” Google API来使地址输入字段自动完成。
网站提供的服务仅限于特定区域,因此我限制了自动完成过滤器。唯一的问题是用户不会被迫使用Google过滤器,而是可以输入任何内容。
如何强制用户从自动完成过滤器中选择某些内容?
如果用户没有从“地点自动完成”列表中选择地点,则只需要做什么,那么您可以简单地做到这一点:
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);
}
});
希望这会有所帮助!