google-maps - Google 地方信息自动完成 - 类型:地理编码、地址、机构 - 无法正常工作
问题描述
我正在尝试在 google 地方自动完成中设置类型,这让我抓狂!
我希望我的用户能够通过输入他们正在寻找的地址或机构来过滤那里的搜索,但由于某种原因它无法正常工作。
我正在使用的代码:
<script defer src="https://maps.googleapis.com/maps/api/js?libraries=places&key=[my_api_key]"></script>
<script>
var searchInputStart = 'start_address';
start_address = new google.maps.places.Autocomplete((document.getElementById(searchInputStart)), {
types: ['geocode'],
types: ['address'],
types: ['establishment']
});
google.maps.event.addListener(start_address, 'place_changed', function () {
var place_start = start_address.getPlace();
document.getElementById('loc_lat_start').value = place_start.geometry.location.lat();
document.getElementById('loc_long_start').value = place_start.geometry.location.lng();
//get_directions to address
set_route();
});
</script>
如果我取出 --> types: [address] --> 它显示正确的机构,但不是地址。
如果我取出 --> types: [establishement] --> 它显示地址正确,但不显示设施。
我的用户如何通过输入地址或机构正确过滤地点?
任何人都可以帮助我吗?我究竟做错了什么?
谢谢你。
解决方案
根据 Google Maps API 文档,您只能在适当的位置设置一种类型的自动完成选项。
types
Array<string>
:要返回的预测类型。有关受支持类型的列表,请参阅开发人员指南。如果未指定任何内容,则返回所有类型。通常只允许使用一种类型。例外情况是您可以安全地混合使用 'geocode' 和 'establishment' 类型,但请注意,这与不指定类型具有相同的效果。
这意味着您可以使用
start_address = new google.maps.places.Autocomplete(
document.getElementById(searchInputStart),
{
types: ['address']
}
);
或者
start_address = new google.maps.places.Autocomplete(
document.getElementById(searchInputStart),
{
types: ['geocode', 'establishment']
}
);
但并非所有这些都在同一个选项对象中。
希望我的回答能澄清你的疑惑。
推荐阅读
- ios - 如何使用 UICollectionView 选择要预取的项目数?
- julia - 在 Julia 1.0.0 上安装 IJulia
- android - 从 Zip 文件中的视频文件创建 VideoView - Android
- javascript - JS中多个项目的运行功能
- c# - 如何将 SVG 图像渲染为单色位图图像?
- elasticsearch - 将 .csv 数据中的经度和纬度映射到 geo_point 类型不起作用
- javascript - 导航的活动类
- sql - SQL Server int 长度属性
- apache-spark - Spark Streaming vs Structured Streaming 性能基准
- oracle - ORA-00932: 不一致的数据类型: 预期 - 获得不同模式的 CLOB