小编Lor*_*ens的帖子

Flutter - 使用 StreamBuilder 为 Google 地图提供实时更新

我目前正在构建一个在 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)),
          )),
Run Code Online (Sandbox Code Playgroud)

然后我在 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);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

最后, initMarker 函数将每个标记添加到由 Google Map 实例获取的 Set 标记中:

void initMarker(chargePoint, documentID) {
var …
Run Code Online (Sandbox Code Playgroud)

google-maps google-maps-markers flutter google-cloud-firestore stream-builder

1
推荐指数
1
解决办法
3229
查看次数