ClusterManager setOnCameraIdleListener

2019-04-08 07:20发布

问题:

While trying to implement use ClusterManager, I noticed that the getMap().setOnCameraChangeListener(clusterManager) is deprecated. Looking at the android-maps-utils sample on Github, I noticed that getMap().setOnCameraIdleListener(mClusterManager);

When I try doing the same, I get an error because the default ClusterManager class doesn't implement GoogleMap.OnCameraIdleListener.

Yet, in my gradle file, I am using what seems to me, the latest maps-util libraries:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4.3'
}

How can I get access to the newest ClusterManager class? Thanks

回答1:

instead of:

mMap.setOnCameraChangeListener(mClusterManager);

do that:

final CameraPosition[] mPreviousCameraPosition = {null};
    googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {
            CameraPosition position = googleMap.getCameraPosition();
            if(mPreviousCameraPosition[0] == null || mPreviousCameraPosition[0].zoom != position.zoom) {
                mPreviousCameraPosition[0] = googleMap.getCameraPosition();
                clusterManager.cluster();
            }
        }
    });

that will work...



回答2:

after play-services-maps 9.4.0 version of the API they change GoogleMap.OnCameraChangeListener to this 3 listeners

GoogleMap.OnCameraMoveStartedListener,

GoogleMap.OnCameraMoveListener,

GoogleMap.OnCameraIdleListener.