java - 如何使用 GeoFirestore (Java/Android) 在谷歌地图上显示多个标记?
问题描述
我正在尝试使用 在我的地图片段上添加多个标记GeoFirestore
,但我不明白该怎么做。我尝试按照他们网站的指导进行操作,但仍然无法获得预期的结果。
如果集合中的多个文档在所需范围内,我希望在地图上显示它们;但是,我不知道应该在哪里实例化标记。
Firestore 中的数据库结构:
GeoFirebase 查询代码:
if (distantCategoryValue != null) {
switch (distantCategoryValue) {
case "6 Km":
CollectionReference geoFirestoreRef = FirebaseFirestore.getInstance().collection("Events");
GeoFirestore geoFirestore = new GeoFirestore(geoFirestoreRef);
GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude()), 6);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDocumentEntered(DocumentSnapshot documentSnapshot, final GeoPoint geoPoint) {
}
@Override
public void onDocumentExited(DocumentSnapshot documentSnapshot) {
}
@Override
public void onDocumentMoved(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {
}
@Override
public void onDocumentChanged(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(Exception e) {
}
});
break;
}
解决方案
前四个覆盖方法中的每一个都有一个DocumentSnapshot
包含一些数据的对象作为第一个参数。根据在特定区域内发生的操作调用每个方法。现在,要获取该数据,您可以使用 DocumentSnapshot 的getData()方法,该方法的返回类型为 a Map<String, Object>
。只需遍历地图并获取l
属性,该属性是一个包含纬度和经度的数组。第二种方法是使用 DocumentSnapshot 的toObject(Class valueType)并将每个DocumentSnapshot
对象转换为一个Event
对象。获得数据后,使用以下代码行将其添加到地图中:
Event event = documentSnapshot.getValue(Event.class);
LatLng latLng = new LatLng(event.getLatitude(), event.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng));
推荐阅读
- python-3.x - Pip 无法安装 - Debian WSL 上的错误“名称解析暂时失败”
- amazon-web-services - 当说必须更换数据库实例时,应用 Terraform 计划是否安全?
- javascript - 从 srcdoc 设置内容时的 iframe onload 事件
- sql-server - 将图像插入 SQL Server 数据库表列
- encryption - 显示不同结果的密钥?
- python - 为MultiIndex DataFrame的交叉选择分配值(numpy的省略号样式)
- git - 如何处理由目标分支中移动的文件引起的合并冲突
- python - Python:使用 map 和 reduce 编写外积
- html - 为什么双引号 urlencoded 为 %22?
- python - 等待 bash 提示出现的 Python 脚本