使用Google Map Cluster显示来自firebase数据库的所有标记

时间:2017-01-13 15:27:42

标签: android firebase-realtime-database google-maps-android-api-2

我可以从Firebase数据库中获取所有标记。但我需要使用Google Maps Clustering库绘制这些标记。目前,我的地图无法显示Firebase数据库中的标记。

这是我添加项目的方法

private void addItems() {
    DatabaseReference mapsrefrence=FirebaseDatabase.getInstance().getReference().child("wr");
    mapsrefrence.child("pubs").addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.hasChildren()) {
                        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                            // TODO: handle the post
                            LocationModel location = postSnapshot.getValue(LocationModel.class);
                            double latitude = location.getLat();
                            double longitude = location.getLng();
                            MyItem offsetItem = new MyItem(latitude, longitude);
                            mClusterManager.addItem(offsetItem);
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                }
            });
}

这是我的群集方法

private void setUpClusterer() {
    // Position the map.
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.28626380000001, 19.104079100000035), 10));

    // Initialize the manager with the context and the map.
    // (Activity extends context, so we can pass 'this' in the constructor.)
    mClusterManager = new ClusterManager<MyItem>(this, mMap);

    // Point the map's listeners at the listeners implemented by the cluster
    // manager.
    mMap.setOnCameraIdleListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);

    // Add cluster items (markers) to the cluster manager.
    addItems();
}

这是我调用我的群集

的地方
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpClusterer();

    //displayClubLocations();
}

1 个答案:

答案 0 :(得分:0)

检查此答案https://stackoverflow.com/a/41706650/1281180

添加/删除项目后,必须调用ClusterManager的cluster()方法。

private void addItems() {
    ...
    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
        ....
        mClusterManager.addItem(offsetItem);
    }

    mClusterManager.cluster();
}