Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Maps v2 Camera Animation

So im not sure if this is a bug or not yet... might be or I may have missed something.

Anyway so here is the link to Google Maps V2 Camera Controls. https://developers.google.com/maps/documentation/android/views#moving_the_camera

The issue :

Animate to a location already animated to does not call onFinish();

How to replicate:

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mLocation.getLatLng(), zoomLevel), 200, new GoogleMap.CancelableCallback() {

                    @Override
                    public void onFinish() {
                        //DO some stuff here!
                    Log.d("animation", "onFinishCalled");

                    }

                    @Override
                    public void onCancel() {
                    Log.d("animation", "onCancel");


                    }
                }); 

This issue may well come about when a user double taps something which called the same animation even if there is a long time between, onFinish will only be called for a successful animation. When the camera is already positioned the onFinish method will not be called!

I could go around doing checks before I do any camera animation but I don't like that as its wasteful.

Any help would be appreciated. Thanks.

like image 935
Aiden Fry Avatar asked Feb 11 '13 16:02

Aiden Fry


People also ask

How do you animate a camera on Google Maps?

To apply a CameraUpdate to the map, you can either move the camera instantly or animate the camera smoothly. To move the camera instantly with the given CameraUpdate , you can call GoogleMap. moveCamera(CameraUpdate) . The CameraUpdate describing where to move the camera.

How do I enable Google Maps API v2?

Click on the “APIs & auth” menu on the left, and from the submenu select APIs. From the list of APIs that appear, scroll down and ensure that Google Maps Android API v2 is set to “On”.


2 Answers

I have the same problem when i want to move camera to the same position, it seems like a bug. Even if old and new position are not the same and the difference is so small , ex: old position.latitude = 94.54284009112, new position.latitude = 94.54284003451, it dosen't work. my solution is to truncate values to get only old_position.latitude = new_position.latitude = 94.54, then i do a test.

There is another problem with moving camera and scroll the map in the same time, for that i disable scroll gesture before moving and enable it on the onFinish() and the onCancel().

public void animateCameraTo(final double lat, final double lng)
{
    _googleMap = getMap();
    CameraPosition camPosition = _googleMap.getCameraPosition();
    if (!((Math.floor(camPosition.target.latitude * 100) / 100) == (Math.floor(lat * 100) / 100) && (Math.floor(camPosition.target.longitude * 100) / 100) == (Math.floor(lng * 100) / 100)))
    {
        _googleMap.getUiSettings().setScrollGesturesEnabled(false);
        _googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)), new CancelableCallback()
        {

            @Override
            public void onFinish()
            {
                _googleMap.getUiSettings().setScrollGesturesEnabled(true);

            }

            @Override
            public void onCancel()
            {
                _googleMap.getUiSettings().setAllGesturesEnabled(true);

            }
        });
    }

}

Hope this helps you ;)

like image 79
user2245576 Avatar answered Nov 12 '22 22:11

user2245576


Your should check the pixel distance, not the geo distance:

LatLngBounds myBounds = YOUR_BOUNDS;
LatLngBounds visibleBounds = map.getProjection().getVisibleRegion().latLngBounds;
Point myCenter = map.getProjection().toScreenLocation(myBounds.getCenter());
Point visibleCenter = map.getProjection().toScreenLocation(visibleBounds.getCenter());
int dist = (int) Math.sqrt(Math.pow(myCenter.x - visibleCenter.x, 2) + Math.pow(myCenter.y - visibleCenter.y, 2));
if (dist > YOUR_THRESHOLD) {
    map.animateCamera(CameraUpdateFactory.newLatLngBounds(myBounds, YOUR_PADDING), new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {
            // do something
        }

        @Override
        public void onCancel() {
            // do something
        }
    });
} else {
    // do something
}
like image 41
artkoenig Avatar answered Nov 12 '22 21:11

artkoenig