flutter - 在不重新启动应用程序的情况下使用颤振更新 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),
),
);
}
}
}
解决方案
推荐阅读
- gradle - Gradle JAR 创建时间
- angular - 使用 Angular mat-expansion 面板时无法在单词之间添加空格
- azure - 在 ARM 模板中使用访问密钥检索在 Azure 容器上装载 Azure 文件共享
- php - 一次获得多个结果
- kubernetes - K8s 服务无法自动绑定相应的端点
- javascript - 导入 React = 'SyntaxError: 不能在模块外使用 import 语句'
- ruby - Ruby:调用父类方法时如何自分配
- python - 带有 python、请求和签名的币安期货
- reactjs - 按下图标时的文本输入 .blur()
- java - 为什么'a'的'Character.getNumericValue'返回10而不是它的Unicode索引97?