BabyishTank
BabyishTank

Reputation: 1482

GoogleMap.CancelableCallback() onFinish() is called before the animation finish

I have a google map screen in Android. I want to move and zoom the camera to a location and get the boundary and zoom level after the animation complete.

This is what I have


        val location = LatLng(33.7701, -118.1937)
        val DEFAULT_ZOOM_LEVEL = 13F
        googleMap.animateCamera(
            CameraUpdateFactory.newLatLngZoom(location, DEFAULT_ZOOM_LEVEL),
            object: GoogleMap.CancelableCallback{
                override fun onFinish() {
                    val visibleRegion = googleMap.projection.visibleRegion

                    Timber.i("zoom level is ${googleMap.cameraPosition.zoom}")
                }

                override fun onCancel() {
                    Timber.i("Animate Camera Canceled")
                }
            }
        )
    }

Most of the time it is working well. onFinish() timber log zoom level is 13.0

But occasionally, onFinish() timber log zoom level is 2.0 This means the animation haven't been completed and the map is not yet zoom in.

I have tried a suggestion from Issue with Android's GoogleMap.CancelableCallback() However, setting the durationMs parameter only slow down the animation, it doesn't make sure onFinish() is call after the animation is complete.

Why is this happen? Is this a bug in google map Android?

I am using com.google.android.gms:play-services-maps:17.0.1 referencing https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera(com.google.android.gms.maps.CameraUpdate,%20com.google.android.gms.maps.GoogleMap.CancelableCallback)

Upvotes: 0

Views: 289

Answers (1)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

Anyway you can use workaround: move camera to target position in onFinish() via moveCamera() call and then get projection:

...
override fun onFinish() {
    googleMap.moveCamera(<YOUR_DESIRED_TARGET_POSITION_AND_ZOOM>)
    val visibleRegion = googleMap.projection.visibleRegion
    Timber.i("zoom level is ${googleMap.cameraPosition.zoom}")
}
...

Upvotes: 1

Related Questions