我正在使用 Material UI 附带的 Google map 地点自动完成功能,但我一直在尝试限制结果。当用户开始输入时,我想要返回的唯一建议是城市、州供他们选择。
以下是 MUI 文档的链接,其中包含示例: Material UI - Google Places Autocomplete
谢谢!
最佳答案
看起来库中的代码正在使用 AutocompleteService.getPlacePredictions 。为了实现您的用例,您需要将值为 ['(cities)']
的 types
属性包含到您的 AutocompleteService.getPlacePredictions 请求中。这将指示地点服务返回与位置或管理区域级别 3 相匹配的结果,如 here 所示。
您可以将其添加到代码示例中的 fetch 中,如下所示:
fetch({ input: inputValue, types: ['(cities)'] }, (results) => {
if (active) {
let newOptions = [];
if (value) {
newOptions = [value];
}
if (results) {
newOptions = [...newOptions, ...results];
}
setOptions(newOptions);
}
});
这是sample code 。确保使用您的 API key 以使示例正常工作。
关于reactjs - Material UI - Google map 自动完成 - 仅限于城市和州?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68138634/