Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnCameraChangeListener() is deprecated

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.

like image 652
Davide3i Avatar asked Aug 02 '16 18:08

Davide3i


4 Answers

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();
                }

            }
        });
like image 102
Barrak90 Avatar answered Nov 11 '22 05:11

Barrak90


In the new model for camera change events, you are correct that the CameraPosition is not passed into the listener.

Instead, you should just use getCameraPosition() whenever you specifically need it (i.e., when the move starts, mid-move, canceled, or finished/returned to idle).

like image 39
ianhanniballake Avatar answered Nov 11 '22 06:11

ianhanniballake


onnCameraChangeListener() is deprecated now, you can use

mMap.setOnCameraMoveStartedListener { reason: Int ->
            when (reason) {
                GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE -> {
                    Log.d("camera", "The user gestured on the map.")
                }
                GoogleMap.OnCameraMoveStartedListener
                        .REASON_API_ANIMATION -> {
                    Log.d("camera", "The user tapped something on the map.")
                }
                GoogleMap.OnCameraMoveStartedListener
                        .REASON_DEVELOPER_ANIMATION -> {
                    Log.d("camera", "The app moved the camera.")
                }
            }
        }

mMap.setOnCameraIdleListener {
    val midLatLng: LatLng = mMap.cameraPosition.target//map's center position latitude & longitude
        }

mMap.setOnCameraMoveStartedListener {

        }

Here mMap is GoogleMap object and I am calling it inside

override fun onMapReady(map: GoogleMap?) {
        mMap = map as GoogleMap
        //your stuff
    }
like image 10
Kishan Solanki Avatar answered Nov 11 '22 05:11

Kishan Solanki


It is advisable to use newly introduced four camera listeners (OnCameraIdleListener, OnCameraMoveListener, OnCameraMoveStartedListener,OnCameraMoveCanceledListener), but if you still want to go with setOnCameraChangeListener use specific version of android-maps-utils(Given below)

compile 'com.google.maps.android:android-maps-utils:0.4.3'

in your module level gradle file.

like image 5
Pravin Divraniya Avatar answered Nov 11 '22 06:11

Pravin Divraniya