我正在开发一个Android应用程序,该应用程序使用Cloud Firestore记录在Google Map上绘制标记。
标记按预期绘制。但是,当我尝试对它们进行颜色编码时,只有最后的记录才被正确地着色。例如,如果我有两个记录需要着色为“ 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));
}
});
任何关于为什么会发生这种情况的建议将不胜感激。