Android - 谷歌地图将相机从位置移动到另一个位置

时间:2013-11-01 08:11:06

标签: android google-maps

很快,我想知道如何通过动画将相机从当前位置移动到另一个位置。这是我的尝试:

 mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(targetPos, 3));
 mapView.animateCamera(CameraUpdateFactory.zoomTo(5), 2000, null);

但Google地图确实会将相机从某个位置移动到目标位置。如何设置它从A移动到目标,哪个A是我可以设置的位置?提前谢谢。

1 个答案:

答案 0 :(得分:16)

查看maps示例中CameraDemoActivity中的代码。 要去一个位置,你需要有一个CameraPosition。

static final CameraPosition SYDNEY =
        new CameraPosition.Builder().target(new LatLng(-33.87365, 151.20689))
                .zoom(15.5f)
                .bearing(0)
                .tilt(25)
                .build();



public void onGoToSydney(View view) {   
    changeCamera(CameraUpdateFactory.newCameraPosition(SYDNEY), new CancelableCallback() {
        @Override
        public void onFinish() {
            Toast.makeText(getBaseContext(), "Animation to Sydney complete", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getBaseContext(), "Animation to Sydney canceled", Toast.LENGTH_SHORT)
                    .show();
        }
    });
}


/**
 * Change the camera position by moving or animating the camera depending on the state of the
 * animate toggle button.
 */
private void changeCamera(CameraUpdate update, CancelableCallback callback) {
    if (mAnimateToggle.isChecked()) {
        if (mCustomDurationToggle.isChecked()) {
            int duration = mCustomDurationBar.getProgress();
            // The duration must be strictly positive so we make it at least 1.
            mMap.animateCamera(update, Math.max(duration, 1), callback);
        } else {
            mMap.animateCamera(update, callback);
        }
    } else {
        mMap.moveCamera(update);
    }
}