在google_maps_flutter中获取可见标记

4

使用Flutter中的Google Maps,我在地图上放置了一些标记。我在Flutter应用程序中有一个单独的RaisedButton,只有当其中一个或多个标记当前在地图上可见时才需要显示该按钮。

如何实现这一点?我找到了一个类似的解决方案,但它是针对Google Maps API而非Flutter中的google_maps_flutter。

2个回答

5

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

0

google_maps_flutter 是一个版本为0.0.3的开发者预览版。请稍等一下,直到更多功能被引入。


请问您是否知道还有其他的方法可用于实现这个功能?我也可以考虑更换插件。 - Dasun Nirmitha
目前还没有接近的进展。请继续关注 google_maps_flutter,因为它是 Flutter 团队的项目。 - Randal Schwartz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接