将地图缩放到某个位置并将其向右移动

Zoom map to a location and move it to right

我想将 GoogleMap 缩放到显示为标记的位置,然后将该标记按像素向右滚动, 目前我像这样组合上面的 2 个动作:

LatLng point = new LatLng(lat,lon);

MarkerOptions endMarker = new MarkerOptions().position(point);

googleMap.addMarker(endMarker);

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15f));

int screenWidth = getScreenWidth();

int margin = (int) getResources().getDimension(R.dimen.double_standard_margin_padding);

new Handler().postDelayed(() -> googleMap.animateCamera(CameraUpdateFactory.scrollBy(-(screenWidth - margin) * 3 / 8, 0), new GoogleMap.CancelableCallback() {
                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void onCancel() {

                    }
                }), 1000);

如您所见,我添加了 2 种方法:

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15f));

googleMap.animateCamera(CameraUpdateFactory.scrollBy(-(screenWidth - margin) * 3 / 8, 0)

而且我不得不延迟移动地图的动作,因为我们不知道地图何时被缩放

有时地图无法移动 (onCancel()) 所以我无法收到预期的 UI。

有人知道如何只用一种方式存档吗?

我遇到过同样的问题好几次,我创建了一个 CameraUpdateAnimator 以给定的延迟(如果需要)执行一系列 CameraUpdates 并允许移动相机或设置动画(参见 my project on GitHub)。我认为它对您的情况很有用:

CameraUpdateAnimator animator = new CameraUpdateAnimator(mMap, this); // Parameters: a GoogleMap instance and an OnCameraIdleListener to return the control to (can be null)
animator.add(CameraUpdateFactory.newLatLngZoom(point, 15f), false, 0); // Move the camera without delay
animator.add(CameraUpdateFactory.scrollBy(-(screenWidth - margin) * 3 / 8, 0), true, 1000); // Animate the camera with a 1000 milliseconds delay
animator.execute();