【发布时间】:2020-03-01 14:11:23
【问题描述】:
我在从 Cloud Firestore 中的数据库查询我的地理位置数据时遇到问题。我浏览了 Youtube 上的文档并得出结论,当我将地理位置数据保存在子集合中时,它最适合我。
这是我的数据库结构:
如果您进入子集合中的文档之一:
数据库本身有一个名为“tourguides”的集合,每个文档都包含基本信息,如旅游名称和旅游所在地区(两者都是字符串)。然后,每个文档都有一个称为“位置”的子集合,其中每个文档都有字符串“名称”和“ID”,还有一个带有纬度和经度数据的地理点。 “Tourguides”集合中的文档显示在 ListView 中。每当我点击其中一个条目时,都会打开一个地图,其中显示来自相应子集合的所有标记。
这是我的 ListView 生成器:
@override
void initState() {
super.initState();
_pointsofinterest = Firestore.instance.collection('tourguides').document('sydney_downtown_guide').col lection('locations').orderBy('name').snapshots();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: _pointsofinterest,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
return new ListView(
children:
snapshot.data.documents.map((DocumentSnapshot document) {
return InkWell(
child: new ListTile(
title: new Text(document['name']),
subtitle: new Text(document['region']),
onTap: () {
return TourMap(
documents: snapshot.data.documents,
initialPosition: const LatLng(-33.868210, 151.208391),
mapController: _mapController,
);
},
),
);
}).toList(),
);
}
},
),
);
}
我将地图放入 StatlessWidget(我不确定。也许这必须是 StatefulWidget?):
class TourMap extends StatelessWidget {
const TourMap({
Key key,
@required this.documents,
@required this.initialPosition,
@required this.mapController,
}) : super(key: key);
final List<DocumentSnapshot> documents;
final LatLng initialPosition;
final Completer<GoogleMapController> mapController;
@override
Widget build(BuildContext context) {
return GoogleMap(
initialCameraPosition: CameraPosition(
target: initialPosition,
zoom: 12,
),
markers: documents
.map((document) => Marker(
markerId: MarkerId(document['placeId']),
icon: BitmapDescriptor.defaultMarker,
position: LatLng(
document['geolocation'].latitude,
document['geolocation'].longitude,
),
infoWindow: InfoWindow(
title: document['location_name'],
),
))
.toSet(),
onMapCreated: (mapController) {
this.mapController.complete(mapController);
},
);
}}
现在我不知道如何在我的 OnTap 函数中设置查询。 Firestore 文档显示,如果我从数据库中查看集合,我总是必须参考特定文档。
例如 (collection/document/collecion)。但在我的查询中,路径中间的“文档”总是不同的,具体取决于用户点击的导游。
对此有什么想法吗?期待您的回复!
更新:我稍微配置了我的数据库结构!我现在使用两个单独的数据库。一个数据库保存有关可用导游的信息(目前只有两个字符串:名称和地区),另一个数据库存储实际的个人位置。 我现在使用 where-queries 根据他们所属的导游姓名获取正确的位置。
查询本身现在适用于 OnTap 函数:
return new ListView(
children:
snapshot.data.documents.map((DocumentSnapshot document) {
return InkWell(
child: new ListTile(
title: new Text(document['name']),
subtitle: new Text(document['region']),
onTap: () {
Firestore.instance.collection('locations').where(
"toActivity",
isEqualTo: document['name'],
)
.snapshots()
.listen((data) =>
data.documents.forEach((doc) => print(doc["location_name"])));
},
),
);
}).toList(),
);
数据库结构:
如果我点击 ListView 中的一个条目,则正确的条目会打印到控制台中。但我需要弹出一个谷歌地图,根据数据库中的“地理位置”值显示适当的标记。
【问题讨论】:
-
这个问题有点宽泛,没有看到你的结构,很难回答。您还应该包含您尝试过的代码,以便我们可以看到思考过程是什么。请更新您的问题。
-
@Jay 谢谢你的回复。我不确定是否必须在我的地图小部件中设置新的 Firebase 连接。我目前无法访问我的子集合。
-
@Jay 我更新了我的代码和我的问题。我的方法现在有点不同了。也许这有助于一些事情。
标签: firebase google-maps flutter google-cloud-firestore