【问题标题】:Google Maps flutter Gestures are not working inside stack谷歌地图颤动手势在堆栈内不起作用
【发布时间】:2020-07-03 10:31:22
【问题描述】:

我正在使用 google_maps_flutter。我的小部件树是 Scaffold -> SingleChildScrollView -> Stack,然后是谷歌地图。我无法使用手势放大和缩小。

SingleChildScrollView( Stack(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).size.height - 10.0,
                width: MediaQuery.of(context).size.width * 1,
                child: _mapView
                    ? GoogleMap(
                        initialCameraPosition: CameraPosition(
                            target: _outletData[0].locationCoords, zoom: 12.0),
                        markers: Set.from(allMarkers),
                        onMapCreated: mapCreated,
                        zoomControlsEnabled: false,
                        zoomGesturesEnabled: true,
                        scrollGesturesEnabled: true,
                        compassEnabled: true,
                        rotateGesturesEnabled: true,
                        mapToolbarEnabled: true,
                        tiltGesturesEnabled: true,
                       
                      )
                    : Container(),
              ),

我也尝试了下面的代码,但仍然无法使用两指触摸放大缩小

Stack(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).size.height - 10.0,
                width: MediaQuery.of(context).size.width * 1,
                child: _mapView
                    ? GoogleMap(
                        initialCameraPosition: CameraPosition(
                            target: _outletData[0].locationCoords, zoom: 12.0),
                        markers: Set.from(allMarkers),
                        onMapCreated: mapCreated,
                        zoomControlsEnabled: false,
                        zoomGesturesEnabled: true,
                        scrollGesturesEnabled: true,
                        compassEnabled: true,
                        rotateGesturesEnabled: true,
                        mapToolbarEnabled: true,
                        tiltGesturesEnabled: true,
                        gestureRecognizers: Set()
                          ..add(Factory<PanGestureRecognizer>(
                              () => PanGestureRecognizer()))
                          ..add(Factory<ScaleGestureRecognizer>(
                              () => ScaleGestureRecognizer()))
                          ..add(Factory<TapGestureRecognizer>(
                              () => TapGestureRecognizer()))
                          ..add(
                            Factory<VerticalDragGestureRecognizer>(
                                () => VerticalDragGestureRecognizer()),
                          ),
                      )
                    : Container(),
              ),

【问题讨论】:

    标签: google-maps flutter


    【解决方案1】:

    SingleChildScrollView 只支持垂直拖动,用于滚动。

    gestureRecognizersEagerGestureRecognizer 添加到GoogleMap 小部件将允许在视图边界内调度所有触摸事件。这包括用于放大和缩小地图的 2 个手指捏合手势。这将需要以下颤振包:

    import 'package:flutter/foundation.dart';
    import 'package:flutter/gestures.dart';
    

    将此添加到您的 GoogleMap 小部件中:

    gestureRecognizers: < Factory < OneSequenceGestureRecognizer >> [
        new Factory < OneSequenceGestureRecognizer > (
            () => new EagerGestureRecognizer(),
        ),
    ].toSet()
    

    这是一个示例代码:

    GoogleMap(
        initialCameraPosition:
        CameraPosition(target: LatLng(-34.397, 150.644)),
        onMapCreated: _onMapCreated,
        zoomControlsEnabled: false,
        zoomGesturesEnabled: true,
        scrollGesturesEnabled: true,
        compassEnabled: true,
        rotateGesturesEnabled: true,
        mapToolbarEnabled: true,
        tiltGesturesEnabled: true,
        gestureRecognizers: < Factory < OneSequenceGestureRecognizer >> [
            new Factory < OneSequenceGestureRecognizer > (
                () => new EagerGestureRecognizer(),
            ),
        ].toSet()
    )
    

    我希望这会有所帮助!祝你的项目好运

    【讨论】:

    • 太棒了!很高兴它有帮助:)
    • 我在检测地图上的手势时遇到了类似的问题。有没有办法检测手势?
    猜你喜欢
    • 2023-04-11
    • 2017-06-12
    • 2017-11-27
    • 2019-02-12
    • 2019-04-21
    • 2019-07-12
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多