【问题标题】:Get Visible Markers in google_maps_flutter在 google_maps_flutter 中获取可见标记
【发布时间】:2019-06-04 01:51:42
【问题描述】:

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

如何做到这一点?我为 Google Maps API 找到了一个 similar solution,但我需要这个用于 Flutter 上的 google_maps_flutter。

【问题讨论】:

    标签: google-maps flutter flutter-dependencies


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      google_maps_flutter 是 0.0.3 版的开发者预览版。请稍等片刻,直到引入更多功能。

      【讨论】:

      • 能否请您告诉我是否有其他方法可以实现这一目标?我也愿意更改插件。
      • 一切都还没有结束。继续关注 google_maps_flutter,因为它是 Flutter 团队的一个项目。
      猜你喜欢
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2019-10-20
      • 2020-10-18
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多