获取地图上显示的标记数量

时间:2018-02-25 16:22:37

标签: java android firebase firebase-realtime-database

  

我刚检索到信息并以标记的形式显示在   地图。我是否可以获得显示标记的计数。

 btnValidate = (Button)findViewById(R.id.validate);
    btnValidate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Clear map from all markers
            mMap.clear();

            //Filter markers by disease
            disease = String.valueOf(spnDisease.getSelectedItem()).toLowerCase();
            location = String.valueOf(spnLocation.getSelectedItem());

            Query dbQuery = FirebaseDatabase.getInstance().getReference().child("diseaseReported").orderByChild("nameReportedAtReportedOn").equalTo(disease+location+txtDate.getText());
            dbQuery.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Log.d("v1", dataSnapshot.toString());
                    Map m = dataSnapshot.getValue(Map.class);
                    double lat = m.getLat();
                    double lng = m.getLng();
                    String reportedAt = m.getReportedAt();
                    String reportedOn = m.getReportedOn();
                    String name = m.getName();
                    String snippetText = "Reported on: " + reportedOn;
                    LatLng c = new LatLng(lat, lng);

                    mMap.addMarker(new MarkerOptions().position(c).title("Disease reported: " + name).snippet(snippetText).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

                    }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });

}

请参阅附带的代码和图片。 代码表示从firebase检索的信息。 Markers on map

1 个答案:

答案 0 :(得分:0)

宣布计数器:

int markersCount = 0;

并将ChildEventListener替换为ValueEventListener,在forEach循环中增加计数器:

            int markersCount = 0;
            Query dbQuery = FirebaseDatabase.getInstance().getReference().child("diseaseReported").orderByChild("nameReportedAtReportedOn").equalTo(disease+location+txtDate.getText());
            dbQuery.addValueEventListener(new ValueEventListener(){
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for(DataSnapshot dataSnapshot : snapshot.getChildren())
                    {
                        Log.d("v1", dataSnapshot.toString());
                        Map m = dataSnapshot.getValue(Map.class);
                        double lat = m.getLat();
                        double lng = m.getLng();
                        String reportedAt = m.getReportedAt();
                        String reportedOn = m.getReportedOn();
                        String name = m.getName();
                        String snippetText = "Reported on: " + reportedOn;
                        LatLng c = new LatLng(lat, lng);

                        mMap.addMarker(new MarkerOptions().position(c).title("Disease reported: " + name).snippet(snippetText).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                        markersCount++;
                    }
                    Log.i("Markers Count", "Total Markers="+markersCount);
                }
            });

另请注意,我在循环结束后打印了计数。