如何在Flutter的PlacePicker包中使用autocompleteComponents参数
Flutter PlacePicker包autocompleteComponents参数使用方法
autocompleteComponents:你希望限制搜索结果的地点分组,目前你可以使用该组件最多过滤5个国家的结果。
使用方法
1. 构造过滤规则列表
autocompleteComponents接收List<Component>类型的参数,每个国家过滤规则通过Component.country(国家二字ISO码)构造,示例如下:
import 'package:google_maps_place_picker/google_maps_place_picker.dart'; // 示例:限制搜索结果仅返回中国、美国、英国的地点 List<Component> autocompleteComponents = [ Component.country("CN"), Component.country("US"), Component.country("GB"), ];
2. 传入PlacePicker组件
将构造好的列表直接赋值给PlacePicker的autocompleteComponents参数即可:
PlacePicker( apiKey: "你的Google Maps API密钥", autocompleteComponents: autocompleteComponents, initialPosition: LatLng(39.9042, 116.4074), // 初始地图中心点 onPlacePicked: (PickResult result) { // 处理选中地点的逻辑 Navigator.of(context).pop(); }, // 其他自定义参数 useCurrentLocation: true, )
注意事项
- 国家码必须使用ISO 3166-1 alpha-2标准的二字大写编码,不可使用中文名称、三字码等其他格式
- 最多可添加5个国家过滤规则,超出部分会被Google Places API自动忽略
- 不传入该参数时,默认返回全球范围的地点搜索结果
内容的提问来源于stack exchange,提问作者Youssef EL Fakir
相关产品推荐
相关产品推荐

