【问题标题】:Use Google Map Cluster to display all markers from firebase database使用 Google Map Cluster 显示来自 firebase 数据库的所有标记
【发布时间】:2017-05-29 00:36:43
【问题描述】:

我能够从 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();
}

【问题讨论】:

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


    【解决方案1】:

    查看这个答案https://stackoverflow.com/a/41706650/1281180

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2021-02-27
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多