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.
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();
}
}
});
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).
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With