【问题标题】:Fit markers on the screen with flutter google maps用颤振的谷歌地图在屏幕上调整标记
【发布时间】:2020-01-25 09:43:39
【问题描述】:

我在玩Google Maps for Flutter。我希望地图视图适合屏幕上的所有标记。基本上,我想和here for android 一样。

我假设cameraTargetBounds 描述了应该适合屏幕边界的区域。但实际上它并不完全适合这个领域,而是介于两者之间。

到目前为止我做了什么:

  @override
  Widget build(BuildContext context) {
    final bloc = Provider.of<BlocMap>(context);
    return SafeArea(
      child: Container(
        child: Stack(
          children: <Widget>[
            Positioned.fill(
              child: StreamBuilder<MapData>(
                  stream: bloc.mapData,
                  builder: (context, snap) {
                    final mapData = snap.data;
                    print("mapData: $mapData");
                    return GoogleMap(
                      padding: EdgeInsets.only(bottom: 42),
                      mapType: _mapType,
                      cameraTargetBounds: mapData?.markers == null
                          ? CameraTargetBounds.unbounded
                          : CameraTargetBounds(_bounds(mapData?.markers)),
                      initialCameraPosition: _cameraPosition,
                      myLocationEnabled: true,
                      myLocationButtonEnabled: true,
                      gestureRecognizers: _buildGestureRecognizer(),
                      onMapCreated: (GoogleMapController controller) {
                        _controller.complete(controller);
                      },
                      markers: mapData?.markers,
                      polylines: mapData?.polylines,
                      polygons: mapData?.polygons,
                      onLongPress: (latLng) {
                        print("LatLng: $latLng");
                      },
                    );
                  }),
            ),
            Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              child: _buildBtnBar(),
            )
          ],
        ),
      ),
    );

    LatLngBounds _bounds(Set<Marker> markers) {
    if (markers == null || markers.isEmpty) return null;
    return _createBounds(markers.map((m) => m.position).toList());
  }


 LatLngBounds _createBounds(List<LatLng> positions) {
    final southwestLat = positions.map((p) => p.latitude).reduce((value, element) => value < element ? value : element); // smallest
    final southwestLon = positions.map((p) => p.longitude).reduce((value, element) => value < element ? value : element);
    final northeastLat = positions.map((p) => p.latitude).reduce((value, element) => value > element ? value : element); // biggest
    final northeastLon = positions.map((p) => p.longitude).reduce((value, element) => value > element ? value : element);
    return LatLngBounds(
        southwest: LatLng(southwestLat, southwestLon),
        northeast: LatLng(northeastLat, northeastLon)
    );
  }

在 Flutter 中实现这一基本功能的预期方法是什么?有什么建议吗?

【问题讨论】:

    标签: google-maps flutter


    【解决方案1】:

    尝试使用控制器将相机移动到新的边界,例如(来自plugin example):

    mapController.moveCamera(
      CameraUpdate.newLatLngBounds(
        LatLngBounds(
          southwest: const LatLng(-38.483935, 113.248673),
          northeast: const LatLng(-8.982446, 153.823821),
        ),
        10.0,
      ),
    );
    

    【讨论】:

      【解决方案2】:

      使用此代码做你想做的事。

      onMapCreated: (GoogleMapController controller) {
         _controller.complete(controller);
      
         setState(() {
            controller.animateCamera(CameraUpdate.newLatLngBounds(_bounds(markers), 50));
         });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-30
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 2019-11-05
        • 2020-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多