首页 > 解决方案 > 如何阻止 GoogleMapController 的 animateCamera 方法连续调用,并且仅在按下当前位置按钮时平移?

问题描述

我有一个显示谷歌地图的应用程序,带有当前位置 FAB,当按下它时,将触发用户当前位置的动画。然而,一旦按下按钮并且地图现在位于用户的位置,就不可能平移谷歌地图,因为每次 Flutter 构建应用程序时,它都会围绕用户当前位置重新定位地图。我的_getCurrentLocation函数是异步的,应该只在按下按钮时触发。

相关代码:

class _MapScreenState extends State<MapScreen> {
  GoogleMapController _controller;
  StreamSubscription _locationSubscription;
  Marker marker; //adds the locator icon to mark current location in real time
  Location _locationTracker = Location();
  Circle circle; // adds a circle around the marker for visibility
  static final _initialCameraPosition = CameraPosition(
    target: LatLng(1.3521, 103.8198),
    zoom: 11.5,
  );

... etc ... //code to convert asset to an icon

    updateMarker(LocationData _locationData, Uint8List imageData) async {
etc etc
      }

//function to get the current location using geocoder api
  void _getCurrentLocation() async {
    print("executing _getCurrentLocation ");
    try {
      Uint8List imageData = await getMarker();
      var currentLocation = await _locationTracker.getLocation();

      updateMarker(currentLocation, imageData);
      if (_locationSubscription != null) {
        _locationSubscription.cancel();
      }

      _locationSubscription =
          _locationTracker.onLocationChanged.listen((LocationData newLocalData) {
        if (_controller != null) {
          _controller
              .animateCamera(CameraUpdate.newCameraPosition(new CameraPosition(
            bearing: 192,
            target: LatLng(newLocalData.latitude, newLocalData.longitude),
            tilt: 0,
            zoom: 14.00,
          )));
          updateMarker(newLocalData, imageData);
        }
      });
    } on PlatformException catch (error) {
      if (error.code == 'PERMISSION DENIED') {
        debugPrint("Permission Denied");
      }
    }
  }



@override
  Widget build(BuildContext context) {
        print("build ");
    return Scaffold(
    
    body: Stack(
      children: <Widget>[
        GoogleMap(
          myLocationButtonEnabled: false,
          zoomControlsEnabled: false,
          initialCameraPosition: _initialCameraPosition,
          markers: Set.of((marker != null) ? [marker] : []),
          circles: Set.of((circle != null) ? [circle] : []),
          onMapCreated: (GoogleMapController controller) {
           _controller = controller;
          },
        ),
        Positioned(
            top: 70.0,
            right: 15.0,
            left: 15.0,
            child: Column(children: <Widget>[
              Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(30),
                ),
                child: TextField(
                    decoration: InputDecoration(
                  labelText: "Going to?",
                  contentPadding:
                      EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                  prefixIcon: Icon(Icons.search),
                  border: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  errorBorder: InputBorder.none,
                  disabledBorder: InputBorder.none,
                )),
              )
            ])),
      ],
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _getCurrentLocation,
      child: Icon(Icons.location_pin),
      backgroundColor: Colors.cyan,
    ));}}

我正在使用onLocationChanged更新 GoogleMapController的_getCurrentLocation函数。整个函数只有在floatingActionButton被按下的时候才会调用,但实际上好像每次flutter构建程序时都会触发?_controller.animateCamera

标签: fluttergoogle-mapsdart

解决方案


推荐阅读