gpt4 book ai didi

google-maps - 在 google_maps_flutter 中获取可见标记

转载 作者:IT王子 更新时间:2023-10-29 06:45:41 24 4
gpt4 key购买 nike

使用 Google Maps for Flutter我在 map 上的不同位置放置了一些标记。我的 Flutter 应用程序上有一个单独的 RaisedButton,只有当这些标记中的一个或多个当前在 map 上可见时,我才需要它可见。

如何实现?我发现有点 similar solution用于 Google Maps API,但我需要它用于 Flutter 上的 google_maps_flutter。

最佳答案

LatLngBounds 具有称为 contains() 的特定方法,它与 GoogleMapControllergetVisibleRegion() 一起工作.

来自官方文档:

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();
},
);
}

关于google-maps - 在 google_maps_flutter 中获取可见标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54107587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com