android 的 google 地图中的聚类不起作用。我究竟做错了什么?

Clustering not working in google maps for android. What am i doing wrong?

这是我在给定位置周围渲染 100 个标记的代码,但无论我做什么,clusters/markers 都不会出现。我怀疑问题是否出在 ClusterManager 的初始化上,mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);

从示例 google 显示 hereClusterManager 被初始化为 mClusterManager = new ClusterManager<MyItem>(this, getMap());

但是我的class继承自fragment,是不是getActivityreturn的Context不对?我认为它期望 com.example.mkallingal.mapapp3.MainActivity.PlaceholderFragment

非常感谢您的帮助,如果您愿意帮助我,我会将其标记为最佳答案。谢谢。

下面是我的代码:

  public static class PlaceholderFragment extends Fragment
    {
        public GoogleMap mMap;
        private ClusterManager<MarkerManager> mClusterManager;
        Context _MapContext;

        private  void setUpMapIfNeeded(View RootView) {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {


                FragmentActivity activity = (FragmentActivity)RootView.getContext();
                FragmentManager manager = activity.getSupportFragmentManager();
                Fragment x= manager.findFragmentById(R.id.container);

                Fragment _ChildFragment= x.getChildFragmentManager().findFragmentById(R.id.map);
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) _ChildFragment)
                        .getMap();
                // Check if we were successful in obtaining the map.
                 if (mMap != null) {
                   clusterUp(5.293842, 101.682636);
                  }
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            _MapContext= container.getContext();
            setUpMapIfNeeded(rootView);
            return rootView;
        }

        private void clusterUp(double lat, double lng)
        {
            LatLng SYDNEY = new LatLng(5.415694, 101.678282);
            LatLng MOUNTAIN_VIEW = new LatLng(lat,lng);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
            mMap.animateCamera(CameraUpdateFactory.zoomIn());
            mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            mMap.setOnCameraChangeListener(mClusterManager);
            mMap.setOnMarkerClickListener(mClusterManager);

            mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);

            for (int i=0;i<100;i++){
                LatLng _NewLocation= getLocationNearCords(5.415694, 101.678282, 1000); //randomly generate 100 cordinates around the location

                MarkerManager offsetItem = new MarkerManager(_NewLocation.latitude,_NewLocation.longitude);
                mClusterManager.addItem(offsetItem);
            }
        }

        public class MarkerManager implements ClusterItem {
            private LatLng mPosition = new LatLng(22.2222, 33.3333);

            public MarkerManager() {

            }

            public MarkerManager(double lat, double lng) {
                mPosition = new LatLng(lat, lng);
            }

            @Override
            public LatLng getPosition() {
                return mPosition;
            }
        }


    }

糟糕,在 ClusterManager 初始化之前附加的事件处理程序应该是这样的,

 mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
 mMap.setOnCameraChangeListener(mClusterManager);
 mMap.setOnMarkerClickListener(mClusterManager);

现在可以使用了...