java - Android Google Maps - 标记颜色编码仅适用于最后的 Firestore 结果
问题描述
我正在开发一个 Android 应用程序,它使用 Cloud Firestore 记录在 Google 地图上绘制标记。
标记按预期绘制。但是,当我尝试对它们进行颜色编码时,只有最后一条记录被正确着色。例如,如果我有两条记录需要着色为“HUE_BLUE”,则只有第二条记录正确着色,而第一个记录在我的循环中的默认选项 (HUE_RED) 中着色。
下面是我用来生成地图标记并对标记进行颜色编码的代码:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
firebase.collection("PointsOfInterest").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentSnapshot doc2:queryDocumentSnapshots) {
poiAddress_id = doc2.getLong("address_id").intValue();
}
}
});
firebase.collection("Events").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for(DocumentSnapshot doc3:queryDocumentSnapshots) {
eventAddress_id = doc3.getLong("address_id").intValue();
}
}
});
firebase.collection("Address").addSnapshotListener(new EventListener<QuerySnapshot>() {
LatLng marker;
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentSnapshot doc:queryDocumentSnapshots) {
latitude = doc.getDouble("address_latitude"); //Obtains the latitude for a given address
longitude = doc.getDouble("address_longitude"); //Obtains the longitude for a given address
address_id = doc.getLong("address_id").intValue();
addressName = doc.getString("address_name");
addressSuburb = doc.getString("address_suburb");
addressState = doc.getString("address_state");
addressPostcode = doc.getLong("address_postcode").intValue();
docID = doc.getId(); //Obtains the name of the specific data record - this is used to label the marker
marker = new LatLng(latitude, longitude); //Creates a map marker utilising the latitude and longitude values retrieved from the database
if (address_id == poiAddress_id) {
markerColour = BitmapDescriptorFactory.HUE_BLUE;
} else if (address_id == eventAddress_id) {
markerColour = BitmapDescriptorFactory.HUE_YELLOW;
} else {
markerColour = BitmapDescriptorFactory.HUE_RED;
}
mMap.addMarker(new MarkerOptions().position(marker).title(docID).snippet(addressName + " " + addressSuburb + ", " + addressState + " " + addressPostcode)
.icon(BitmapDescriptorFactory.defaultMarker(markerColour)));
}
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}
});
任何关于为什么会发生这种情况的建议将不胜感激。
解决方案
推荐阅读
- django - /servicesurvey/8:pk 处的 Django TypeError
- java - 嗨,我试图在我的程序输入中只接受数字,但我收到了错误消息
- flutter - Flutter:使文本以不同的大小响应
- django - Django模型字段默认值与PK相同
- delphi - Delphi:32 位与 64 位中的字符串 [255]
- swift - 线程 1:使用 let realm = try! 发出信号 SIGABRT Xcode 11 升级后的 Realm()
- laravel - 图片未上传到 laravel voyager 指定的目录
- javascript - 当我尝试编译时,我的 JavaScript 代码出错
- c# - 如何使用 Azure DevOps 发布管道中的内置 DLL 运行我的测试?
- python - 当我从谷歌抓取图像时,图像 src 包含无