更改标记后,Flutter Google 地图不会重新加载

发布于 2025-01-15 03:47:53 字数 2280 浏览 1 评论 0原文

我正在使用谷歌地图小部件,当我第一次使用静态标记渲染地图时,它会显示所有标记,但是当我从 API 获取数据并尝试向地图添加新标记时,它不会显示它们。
在我正在使用的代码下面:

class Map extends StatefulWidget {
  final Position initialPosition;
  final List<Marker> allSitesMarkers;
  final List<Site> placesSite;
  final List<String> mapMarkersCategories;
  const Map(this.initialPosition, this.allSitesMarkers, this.placesSite,
      this.mapMarkersCategories,
      {Key key})
      : super(key: key);
  @override
  State<StatefulWidget> createState() => MapState();
}

class MapState extends State<Map> {
  Completer<GoogleMapController> _controller = Completer();

  String currentCategoryFilter;
  bool filtred = false;
  Set<Marker> _markers = Set();

  List<Marker> markersToShow;
  @override
  void initState() {
    super.initState();
    refreshMarkers();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: (widget.placesSite.isNotEmpty)
          ? Stack(
              children: [
                GoogleMap(
                  initialCameraPosition: const CameraPosition(
                      target: LatLng(36.798, 10.1717), zoom: 15),
                  mapType: MapType.normal,
                  myLocationEnabled: true,
                  markers: _markers,
                  onMapCreated: (GoogleMapController controller) {
                    controller.setMapStyle(
                        '[ //... ]');

                    _controller.complete(controller);
                  },
                ),
                _chips()
              ],
            )
          : const NoListMap(),
    );
  }


  Widget _chips() {
    //..
  }

  filterSites(String category) {
    setState(() {
      _markers.clear();
      filtred = true;
      markersToShow = widget.allSitesMarkers
          .where((element) => element.infoWindow.title == category)
          .toList();
    });
    refreshMarkers();
  }

  refreshMarkers() {
    if (filtred) {
      setState(() {
        _markers.clear();
        _markers.addAll(markersToShow);
      });
    } else {
      setState(() {
        _markers.clear();
        _markers.addAll(widget.allSitesMarkers);
      });
    }
  }
}

我正在使用:
颤振2.10.2
飞镖2.16.1
谷歌地图颤振:2.0.9

I'm working with the google map widget, when I render the map for the first time with static markers it displays all the markers, but when I get the data from API and try to add new markers to the map, it won't display them.
below the code that i'm using :

class Map extends StatefulWidget {
  final Position initialPosition;
  final List<Marker> allSitesMarkers;
  final List<Site> placesSite;
  final List<String> mapMarkersCategories;
  const Map(this.initialPosition, this.allSitesMarkers, this.placesSite,
      this.mapMarkersCategories,
      {Key key})
      : super(key: key);
  @override
  State<StatefulWidget> createState() => MapState();
}

class MapState extends State<Map> {
  Completer<GoogleMapController> _controller = Completer();

  String currentCategoryFilter;
  bool filtred = false;
  Set<Marker> _markers = Set();

  List<Marker> markersToShow;
  @override
  void initState() {
    super.initState();
    refreshMarkers();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: (widget.placesSite.isNotEmpty)
          ? Stack(
              children: [
                GoogleMap(
                  initialCameraPosition: const CameraPosition(
                      target: LatLng(36.798, 10.1717), zoom: 15),
                  mapType: MapType.normal,
                  myLocationEnabled: true,
                  markers: _markers,
                  onMapCreated: (GoogleMapController controller) {
                    controller.setMapStyle(
                        '[ //... ]');

                    _controller.complete(controller);
                  },
                ),
                _chips()
              ],
            )
          : const NoListMap(),
    );
  }


  Widget _chips() {
    //..
  }

  filterSites(String category) {
    setState(() {
      _markers.clear();
      filtred = true;
      markersToShow = widget.allSitesMarkers
          .where((element) => element.infoWindow.title == category)
          .toList();
    });
    refreshMarkers();
  }

  refreshMarkers() {
    if (filtred) {
      setState(() {
        _markers.clear();
        _markers.addAll(markersToShow);
      });
    } else {
      setState(() {
        _markers.clear();
        _markers.addAll(widget.allSitesMarkers);
      });
    }
  }
}

I'm using:
Flutter 2.10.2
Dart 2.16.1
google_maps_flutter : 2.0.9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2025-01-22 03:47:53

您的 _markers 是一个 Set,在 setState 中,您使用 clearaddAll 来清空它并向集合中添加新项目。但是,当您这样做时,_markers 的值不会改变,因此不会触发重建。

发生这种情况是因为 _markers 保存了对 Set引用(如列表、对象等)。

您需要为 _markers 分配一个新值来触发重建,例如:

setState(() {
  _markers = markersToShow.toSet();
});

这实际上会更改 _markers 中保存的引用。

Your _markers is a Set, and in setState you use clear and addAll to empty it and to add new items to the set. But the value of _markers does not change when you do so, therefore it will not trigger a rebuild.

This happens because _markers holds a reference to the Set (like lists, objects etc.).

You need to assign a new value to _markers to trigger a rebuild, for example:

setState(() {
  _markers = markersToShow.toSet();
});

This will actually change the reference kept in _markers.

寄风 2025-01-22 03:47:53

这就是我更新职位的方法

setState(() {
    mapController.animateCamera(CameraUpdate.newCameraPosition(
        CameraPosition(
            target: LatLng(widget.siteDetails?.location?.lat ?? 8.5555187,
                widget.siteDetails?.location?.long ?? 76.8836149),
            zoom: 11)));
    markers = <Marker>[
      Marker(
          markerId: const MarkerId('MarkId'),
          position: LatLng(widget.siteDetails?.location?.lat ?? 0,
              widget.siteDetails?.location?.long ?? 0),
          infoWindow: InfoWindow.noText)
    ];
  });

This is what I Did to update the posistion

setState(() {
    mapController.animateCamera(CameraUpdate.newCameraPosition(
        CameraPosition(
            target: LatLng(widget.siteDetails?.location?.lat ?? 8.5555187,
                widget.siteDetails?.location?.long ?? 76.8836149),
            zoom: 11)));
    markers = <Marker>[
      Marker(
          markerId: const MarkerId('MarkId'),
          position: LatLng(widget.siteDetails?.location?.lat ?? 0,
              widget.siteDetails?.location?.long ?? 0),
          infoWindow: InfoWindow.noText)
    ];
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文