【问题标题】:Zoom map to a location and move it to right将地图缩放到某个位置并将其向右移动
【发布时间】:2016-11-22 07:04:33
【问题描述】:

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

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);

如你所见,我添加了两种方法来做到这一点:

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

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

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

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

有人知道如何仅以一种方式存档吗?

【问题讨论】:

    标签: android google-maps zooming move


    【解决方案1】:

    我多次遇到同样的问题,我创建了一个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();
    

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 2016-01-06
      • 1970-01-01
      • 2021-11-23
      • 2012-09-10
      • 2019-01-16
      • 2018-03-20
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多