google-maps - 在 google_maps_flutter 中获取可见标记
问题描述
使用Google Maps for Flutter我在地图上的不同位置放置了一些标记。我的 Flutter 应用程序上有一个单独的 RaisedButton,只有当其中一个或多个标记当前在地图上可见时,我才需要它可见。
如何做到这一点?我为 Google Maps API 找到了一个有点类似的解决方案,但我需要在 Flutter 上使用 google_maps_flutter。
解决方案
LatLngBounds
具有特定的方法,称为contains()
which 与GoogleMapController
's一起工作getVisibleRegion()
。
来自官方文档:
contains(LatLng point) → bool
Returns whether this rectangle contains the given LatLng.
用法:
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _positionCenter = CameraPosition(
target: LatLng(40.730610, -73.935242),
zoom: 3.5,
);
Future<LatLngBounds> _getVisibleRegion() async {
final GoogleMapController controller = await _controller.future;
final LatLngBounds bounds = await controller.getVisibleRegion();
return bounds;
}
@override
Widget build(BuildContext context) {
void checkMarkers() async {
LatLngBounds bounds = await _getVisibleRegion();
Set<Marker> markers = Set<Marker>.of(markers.values);
markers.forEach((marker) {
print('Position: ${ marker.position } - Contains: ${ bounds.contains(marker.position) }');
});
}
return GoogleMap(
mapType: MapType.normal,
markers: Set<Marker>.of(markers.values),
initialCameraPosition: _positionCenter,
onCameraIdle: () {
checkMarkers();
},
onMapCreated: (GoogleMapController controller) async {
_controller.complete(controller);
checkMarkers();
},
);
}
推荐阅读
- java - 用于增加堆大小的 java Xms 命令无法正常工作
- python - 如何在两个模式之间找到输出线(它们是相同的)
- php - 根据 WooCommerce 中的产品税等级调整运输等级
- c - 为什么这个 cgi 脚本不起作用,但在 /var/www/cgi-bin/ 中执行时,它作为独立程序完美运行?
- c# - ASP.NET WebControl 返回对象
- c# - 为什么这个三元语句在简写时会返回误报?
- python - 无论如何,是否可以使用 Python 在 Windows 上通过 PID 或名称获取进程信息?
- laravel - Laravel 通知传递数据
- r - 当默认过滤器处于活动状态时下载所有数据
- java - Java中同步文件写入机制到多个文件中同时写入