材料UI-谷歌地图自动完成-仅限于城市和州



我正在使用Material UI附带的Google Maps Places Autocomplete,我一直在尝试限制结果。当用户开始键入时,我希望返回的唯一建议是由他们选择的城市、州。

以下是MUI文档的链接,示例如下:材料UI-谷歌放置自动完成

谢谢!

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

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

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

这是示例代码。请确保使用您的API密钥使示例工作。

最新更新