Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - google map move camera from position to another

Shortly, I want to know how I can move the camera from current position to another one with animation. Here is my try:

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

But Google map does move camera from some position to the target. How can I set it move from A to target, which A is some position I can set? Thanks in advance.

like image 752
ductran Avatar asked Nov 01 '13 08:11

ductran


1 Answers

Look at the code in CameraDemoActivity in the maps sample. To go to a position you need to have a 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);
    }
}
like image 139
danny117 Avatar answered Sep 19 '22 17:09

danny117