简体   繁体   English

Android谷歌地图v2相机动画

[英]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. 无论如何,这里是谷歌地图V2相机控件的链接。 https://developers.google.com/maps/documentation/android/views#moving_the_camera https://developers.google.com/maps/documentation/android/views#moving_the_camera

The issue : 问题 :

Animate to a location already animated to does not call onFinish(); 动画到已经动画化的位置不会调用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. 当用户双击一些调用相同动画的东西时即使间隔时间很长,也可能会出现这个问题,onFinish只会被调用成功动画。 When the camera is already positioned the onFinish method will not be called! 当相机已经定位时,将不会调用onFinish方法!

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. 谢谢。

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. 即使旧的和新的位置不一样且差别很小,例如:old position.latitude = 94.54284009112,new position.latitude = 94.54284003451,它不起作用。 my solution is to truncate values to get only old_position.latitude = new_position.latitude = 94.54, then i do a test. 我的解决方案是截断值只得到old_position.latitude = new_position.latitude = 94.54,然后我做一个测试。

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(). 移动相机并同时滚动地图还有另一个问题,因为我在移动之前禁用滚动手势并在onFinish()和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 ;) 希望这可以帮助你;)

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
}

The only solution I have found is doing the wasteful pixel value checks, setting up a bogus (but close) CameraUpdate object and making a nested animateCamera() call using the bogus CameraUpdate first and then calling another cameraUpdate() inside the first onFinish() with the correct CameraUpdate. 我发现的唯一解决方案是进行浪费的像素值检查,设置伪造(但关闭)CameraUpdate对象并首先使用伪造的CameraUpdate进行嵌套的animateCamera()调用,然后在第一个onFinish()内调用另一个cameraUpdate()使用正确的CameraUpdate。 I installed the Google MAP v2 update today but it didn't help. 我今天安装了Google MAP v2更新,但它没有帮助。 This is a problem on device rotation for me. 对我来说这是设备轮换的问题。 I am displaying bounding rectangles and the centroid doesn't change on rotation so the animateCamera() doesn't work and when the device is rotated the selected rectangle may be partially off the screen. 我正在显示边界矩形,并且质心在旋转时不会改变,因此animateCamera()不起作用,并且当设备旋转时,所选矩形可能部分偏离屏幕。

Did you find another solution yet? 你找到了另一个解决方案吗? Thanks 谢谢

Here's another workaround: 这是另一种解决方法:

You do know the animation duration of your map animations. 您确实知道地图动画的动画持续时间。 So you can post a delayed runnable with a delay of the animation duration + some offset and there you can check if the finished/canceld listener was called... if not you can fetch it there and call the apropriate listener 所以你可以发布一个延迟的runnable,延迟动画持续时间+一些偏移量,你可以检查是否已经调用了完成的/ canceld监听器...如果没有你可以在那里获取它并调用适当的监听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM