【问题标题】:Flutter/Dart Google Maps re-render widget upon mapcontroller updateFlutter/Dart Google Maps 在 mapcontroller 更新时重新渲染小部件
【发布时间】:2019-06-09 23:54:39
【问题描述】:

如何使 Google 地图小部件在地图控制器进行更改时重新呈现?

@override
  Widget build(BuildContext context) {
    return GoogleMap(
      onMapCreated: _onMapCreated,
      options: GoogleMapOptions(
        compassEnabled: true,
        cameraPosition: CameraPosition(
          target: LatLng(-36.8446085, 174.7547239),
          zoom: 15.0,
        ),
      ),
    );
  }

该方法通过使用地图控制器进行更新,根据用户的当前位置添加标记和动画缩放。此方法由在此 google 地图渲染小部件的父小部件处触发 StreamController 的 FAB 运行。

void _populateUserLocation() {
    _getLocation().then((location) {
      setState(() {
        userLocation = LatLng(location['latitude'], location['longitude']);
      });
    });
    _mapController.addMarker(
      MarkerOptions(
        position: userLocation,
        infoWindowText: InfoWindowText("!", "Your are here"),
      )
    );
    _mapController.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
          target: userLocation,
          tilt: 50.0,
          zoom: 20.0,
        )
      )
    );
  }

目前,这仅在第二台 FAB 印刷机上按预期工作。这意味着地图没有根据地图控制器所做的更改适当地重新渲染。

【问题讨论】:

  • 您是否找到了解决地图在新位置重新渲染的好方法?

标签: google-maps dart flutter


【解决方案1】:

我认为你应该这样做:

Future<void> _populateUserLocation() async {
 final location = await _getLocation(); // <-- async call waits until _getLocation completes
 userLocation = LatLng(location['latitude'], location['longitude']);
 setState(() {
   _mapController.addMarker(
      MarkerOptions(
        position: userLocation,
        infoWindowText: InfoWindowText("!", "Your are here"),
      )
    );
    _mapController.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
          target: userLocation,
          tilt: 50.0,
          zoom: 20.0,
        )
      )
    );
  });
}

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2021-11-13
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多