【发布时间】:2020-08-12 08:04:11
【问题描述】:
我目前正在构建一个在 Google 地图上显示标记的应用程序。标记的位置是从 Firestore 数据库中提取的。目前,我正在成功检索和显示所有标记。但是,我需要根据数据库中的布尔值更新每个标记的图标。
这是我当前显示标记的方式。
我在主构建中创建地图:
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: mapToggle
? GoogleMap(
onMapCreated: onMapCreated,
compassEnabled: false,
polylines: route,
initialCameraPosition: CameraPosition(
target: LatLng(currentLocation.latitude,
currentLocation.longitude),
zoom: 12),
markers: markers,
)
: Center(
child: Text('Loading... Please wait...',
style: TextStyle(fontSize: 20.0)),
)),
然后我在 initState 中调用这个函数来获取我的标记:
chargePoints() {
_database.collection('charge_points').getDocuments().then((docs) {
if (docs.documents.isNotEmpty) {
for (int i = 0; i < docs.documents.length; i++) {
initMarker(docs.documents[i].data, docs.documents[i].documentID);
}
}
});
最后,initMarker 函数将每个标记添加到 Set 标记中,由 Google Map 实例获取:
void initMarker(chargePoint, documentID) {
var markerIdVal = documentID;
final MarkerId markerId = MarkerId(markerIdVal);
// Creating a new Charge Point Marker
final Marker marker = Marker(
markerId: markerId,
icon: markerIcon,
position: LatLng(chargePoint['location'].latitude,
chargePoint['location'].longitude),
infoWindow: InfoWindow(
title: chargePoint['name'], snippet: chargePoint['type']),
onTap: () {
findBookings(context, chargePoint, documentID);
});
setState(() {
markers.add(marker);
});
所以我的问题是;当“占用”字段已更改时,如何将此数据作为流返回,并提供一个更新相应标记图标的侦听器?
【问题讨论】:
标签: google-maps flutter google-cloud-firestore google-maps-markers stream-builder