首页 > 解决方案 > 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));
        }
    });

任何关于为什么会发生这种情况的建议将不胜感激。

标签: javaandroidgoogle-mapsgoogle-cloud-firestore

解决方案


推荐阅读