Flutter - 一段时间后自动刷新地图上标记的图标
Flutter - auto-refresh marker's icon on map after a period of time
我有一些标记,标记的图标根据 firebase 字段(称为状态)而变化。 status 是一个布尔值 (true/false)。外部应用程序经常在数据库中更改此状态值,因此我需要能够根据字段的当前值更新图标。如何自动更新 UI 上的标记?
这是我的代码:
BitmapDescriptor customFreeLotIcon, customOccupiedLotIcon;
Future<List<Marker>> _createMarkersForLotsAndParkings() async{
List<Marker> markersList = [];
int markerId = 0;
QuerySnapshot subDocuments = await parkingDocReference.collection("lots").get();
List<DocumentSnapshot> subDocumentsSnapshots = subDocuments.documents;
for (DocumentSnapshot subDoc in subDocumentsSnapshots){
String subDocId = subDoc.documentID;
DocumentSnapshot snapshot = await parkingDocReference.collection("lots")
.document(subDocId).get();
print(snapshot.get('location').latitude);
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(snapshot.get('location').latitude,
snapshot.get('location').longitude),
onTap: () => _changeMap(LatLng(
snapshot.get('location').latitude,
snapshot.get('location').longitude)),
icon: snapshot.get('status') == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
}
}
return Future.value(markersList);
}
createCustomImageForParkingsMarkers(context){
if (customFreeLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/freeLots.png')
.then((icon) {
setState(() {
customFreeLotIcon = icon;
});
});
}
else if (customOccupiedLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/occupiedLots.png')
.then((icon) {
setState(() {
customOccupiedLotIcon = icon;
});
});
}
}
尝试添加监听器:
BitmapDescriptor customFreeLotIcon, customOccupiedLotIcon;
Future<List<Marker>> _createMarkersForLotsAndParkings() async{
List<Marker> markersList = [];
int markerId = 0;
QuerySnapshot subDocuments = await parkingDocReference.collection("lots").get();
List<DocumentSnapshot> subDocumentsSnapshots = subDocuments.documents;
for (DocumentSnapshot subDoc in subDocumentsSnapshots){
String subDocId = subDoc.documentID;
DocumentSnapshot snapshot = await parkingDocReference.collection("lots")
.document(subDocId).snapshots()
.listen((DocumentSnapshot documentSnapshot) async {
Map<String, dynamic> document = documentSnapshot.data();
print(document['location'].latitude);
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(document['location'].latitude,
document['location'].longitude),
onTap: () => _changeMap(LatLng(
document['location'].latitude,
document['location'].longitude)),
icon: document['status'] == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
}
}
}
return Future.value(markersList);
}
createCustomImageForParkingsMarkers(context){
if (customFreeLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/freeLots.png')
.then((icon) {
setState(() {
customFreeLotIcon = icon;
});
});
}
else if (customOccupiedLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/occupiedLots.png')
.then((icon) {
setState(() {
customOccupiedLotIcon = icon;
});
});
}
}
在此处添加Setstate()
:
setState(() {
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(document['location'].latitude,
document['location'].longitude),
onTap: () => _changeMap(LatLng(
document['location'].latitude,
document['location'].longitude)),
icon: document['status'] == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
});
我有一些标记,标记的图标根据 firebase 字段(称为状态)而变化。 status 是一个布尔值 (true/false)。外部应用程序经常在数据库中更改此状态值,因此我需要能够根据字段的当前值更新图标。如何自动更新 UI 上的标记?
这是我的代码:
BitmapDescriptor customFreeLotIcon, customOccupiedLotIcon;
Future<List<Marker>> _createMarkersForLotsAndParkings() async{
List<Marker> markersList = [];
int markerId = 0;
QuerySnapshot subDocuments = await parkingDocReference.collection("lots").get();
List<DocumentSnapshot> subDocumentsSnapshots = subDocuments.documents;
for (DocumentSnapshot subDoc in subDocumentsSnapshots){
String subDocId = subDoc.documentID;
DocumentSnapshot snapshot = await parkingDocReference.collection("lots")
.document(subDocId).get();
print(snapshot.get('location').latitude);
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(snapshot.get('location').latitude,
snapshot.get('location').longitude),
onTap: () => _changeMap(LatLng(
snapshot.get('location').latitude,
snapshot.get('location').longitude)),
icon: snapshot.get('status') == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
}
}
return Future.value(markersList);
}
createCustomImageForParkingsMarkers(context){
if (customFreeLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/freeLots.png')
.then((icon) {
setState(() {
customFreeLotIcon = icon;
});
});
}
else if (customOccupiedLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/occupiedLots.png')
.then((icon) {
setState(() {
customOccupiedLotIcon = icon;
});
});
}
}
尝试添加监听器:
BitmapDescriptor customFreeLotIcon, customOccupiedLotIcon;
Future<List<Marker>> _createMarkersForLotsAndParkings() async{
List<Marker> markersList = [];
int markerId = 0;
QuerySnapshot subDocuments = await parkingDocReference.collection("lots").get();
List<DocumentSnapshot> subDocumentsSnapshots = subDocuments.documents;
for (DocumentSnapshot subDoc in subDocumentsSnapshots){
String subDocId = subDoc.documentID;
DocumentSnapshot snapshot = await parkingDocReference.collection("lots")
.document(subDocId).snapshots()
.listen((DocumentSnapshot documentSnapshot) async {
Map<String, dynamic> document = documentSnapshot.data();
print(document['location'].latitude);
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(document['location'].latitude,
document['location'].longitude),
onTap: () => _changeMap(LatLng(
document['location'].latitude,
document['location'].longitude)),
icon: document['status'] == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
}
}
}
return Future.value(markersList);
}
createCustomImageForParkingsMarkers(context){
if (customFreeLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/freeLots.png')
.then((icon) {
setState(() {
customFreeLotIcon = icon;
});
});
}
else if (customOccupiedLotIcon == null) {
ImageConfiguration configuration = createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(configuration, 'assets/occupiedLots.png')
.then((icon) {
setState(() {
customOccupiedLotIcon = icon;
});
});
}
}
在此处添加Setstate()
:
setState(() {
markersList.add(
Marker(
markerId:MarkerId(markerId.toString()),
position: LatLng(document['location'].latitude,
document['location'].longitude),
onTap: () => _changeMap(LatLng(
document['location'].latitude,
document['location'].longitude)),
icon: document['status'] == true ? customOccupiedLotIcon : customFreeLotIcon ),
);
markerId++;
});