首页 > 解决方案 > 在不重新启动应用程序的情况下使用颤振更新 GoogleMap 中的标记

问题描述

如何在颤动中更新谷歌地图中的标记,我正在使用flutter_bloc包,这段代码工作得很好,但我需要重新启动我的应用程序才能根据事件更改更新标记

编辑:当我重新启动应用程序并从 initState() 调用事件而不是单击按钮时,一切正常!

在这里,我将事件添加到集团:

return BlocBuilder<AuthBloc, AuthStates>(
      builder: (context, state){
        if(state is AuthInitialState) {
          return Container(width: 0, height: 0);
        } else if (state is AuthenticatedState) {
          return  ConvexAppBar(
            height: 60,
            items: [
              TabItem(icon: Icons.outlined_flag, title: 'مفقود'),
              TabItem(icon: Icons.flag, title: 'موجود'),
              TabItem(icon: Icons.location_searching, title: 'الكل'),
              TabItem(icon: Icons.people, title: 'الحساب'),
              TabItem(icon: Icons.add, title: 'إضافة'),
            ],
            initialActiveIndex: 2,
            onTap: (int i) {
              if(i == 0) {
                getBloc.add(GetLostEvent());
              } else if (i == 1) {
                getBloc.add(GetExistEvent());

这是 map_screen 中的代码,其中显示带有标记的地图:

 BlocBuilder<GetPlacesBloc, GetPlacesStates>(
                builder: (context, state) {
                  if(state is GetPlacesLoadingState) {
                    return LoadingIndicator(message: state.message,);
                  } else if (state is GetPlacesSuccessState) {
                    return buildMap(state.markers);

这是同一文件中的 buildMap:

 Widget buildMap(Set<Marker> marker) {
        return GoogleMap(
          markers: marker,
          mapType: MapType.satellite,
          initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 13.0,
          ),
          onMapCreated: _onMapCreated,
        );
      }

bloc.dart 我添加了标记:

Set<Marker> markers = {};

然后是 mapEventToState 的代码:

else if (event is GetExistEvent) {
      yield GetPlacesLoadingState(message: "جاري تحميل الموجودات");
      try {
        List<PlaceModel> places = await repo.getExistPlaces();
        //TODO : markers not updated
         getAllMarkers(places);
        yield GetExistSuccessState(places: places, markers: markers);
      } catch (e) {
        yield GetPlacesErrorState(message: "لا يمكن جلب المواقع");
      }
    } else if (event is GetLostEvent) {
      yield GetPlacesLoadingState(message: "جاري تحميل المفقودات");
      try {
        List<PlaceModel> places = await repo.getLostPlaces();
         getAllMarkers(places);
        yield GetLostSuccessState(places: places, markers: markers);


//TODO
  getAllMarkers(List<PlaceModel> places) {
    for (int i = 0; i < places.length; i++) {
      markers.add(
        Marker(
          markerId: MarkerId('${places[i].pid}'),
          position:
              LatLng(double.parse(places[i].lat), double.parse(places[i].lon)),
          icon:
              BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueAzure),
          infoWindow: InfoWindow(
              title: '${places[i].title}', snippet: '${places[i].details}'),
          onTap: () => tileScrollTo(i),
        ),
      );
    }
  }

}

标签: fluttergoogle-mapsgoogle-maps-markersflutter-bloc

解决方案


推荐阅读