强制用户仅使用位置自动完成结果
Force user to use only place autocomplete results
我正在使用“Places”Google API 使地址输入字段自动完成。
该网站提供的服务仅限于特定区域,因此我限制了自动完成过滤器。唯一的问题是用户不会被迫使用 google 过滤器,而是可以输入任何内容。
如何强制用户select来自自动完成过滤器的内容?
这是我的 code
的 JsFiddle
如果您只需要在用户没有select地点自动完成列表中的地点时什么都不做,那么您只需这样做:
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);
}
});
这是jsfiddle based off of Google's example。
希望对您有所帮助!
我正在使用“Places”Google API 使地址输入字段自动完成。
该网站提供的服务仅限于特定区域,因此我限制了自动完成过滤器。唯一的问题是用户不会被迫使用 google 过滤器,而是可以输入任何内容。
如何强制用户select来自自动完成过滤器的内容?
这是我的 code
如果您只需要在用户没有select地点自动完成列表中的地点时什么都不做,那么您只需这样做:
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);
}
});
这是jsfiddle based off of Google's example。
希望对您有所帮助!