OnCameraChangeListener() is deprecated

Davide3i picture Davide3i · Aug 2, 2016 · Viewed 32.9k times · Source

Today, looking back at my old code, I've found out that OnCameraChangeListener() is now deprecated.

I'm finding difficult to understand how to fix this piece of code of mine:

mGoogleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        // Cleaning all the markers.
        if (mGoogleMap != null) {
            mGoogleMap.clear();
        }

        mPosition = cameraPosition.target;
        mZoom = cameraPosition.zoom;

        if (mTimerIsRunning) {
            mDragTimer.cancel();
        }

        mDragTimer.start();
        mTimerIsRunning = true;
    }
});

The new listener (aka OnCameraMoveListener()) method onCameraMove() doesn't have a CameraPosition cameraPosition input variable, so I'm pretty lost: is there a way to recycle my old code?

Here are some references.

Answer

Barrak90 picture Barrak90 · Aug 3, 2016

In play-services-maps 9.4.0 version of the API, They replaced GoogleMap.OnCameraChangeListener() with three camera listeners :

  • GoogleMap.OnCameraMoveStartedListener
  • GoogleMap.OnCameraMoveListener
  • GoogleMap.OnCameraIdleListener

Based on your code, I think you need to use GoogleMap.OnCameraIdleListener and GoogleMap.OnCameraMoveStartedListener like this:

mGoogleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
            @Override
            public void onCameraMoveStarted(int i) {
                mDragTimer.start();
                mTimerIsRunning = true;
            }
        });

        mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                // Cleaning all the markers.
                if (mGoogleMap != null) {
                    mGoogleMap.clear();
                }

                mPosition = mGoogleMap.getCameraPosition().target;
                mZoom = mGoogleMap.getCameraPosition().zoom;

                if (mTimerIsRunning) {
                    mDragTimer.cancel();
                }

            }
        });