Search code examples
androidgoogle-mapszoomingmarkerbounds

CameraPosition with bounds using GoogleMap


I'm having a bit of a struggle using CameraPosition because I can't have a proper zoom. For me, a proper zoom would be the distance between a Marker and my current position. However, I managed to get a proper zoom using CameraUpdateFactory, but the I lost all the other attributes (orientation (looking always north) and bird-eye (45 degrees view)).

I'm balanced between this (doesn't have the right zoom):

CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(current_location)
                        .zoom(mGoogleMap.getCameraPosition().zoom)
                        .bearing(location.getBearing())
                        .tilt(birdEyesAngle)
                        .build();

and this (right zoom, but missing other attributes):

CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);

Is there any way to have both correct zoom and correct orientation/bird-eye ?

Hope you'll help, Thanks


Solution

  • Try this:

    Get the CameraUpdate object first using the newLatLngBounds method:

    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);
    

    The following code I put in my onMapReady() method:

            LatLng pos = new LatLng(51.516667, 12.388889);
            LatLng pos1 = new LatLng(53.516667, 14.388889);
    
            MarkerOptions markerOptions = setUserMarker(pos);
            if(markerOptions != null) {
                markerOptions.title(campusLocationName);
                mMap.addMarker(markerOptions);
            }
    
            LatLngBounds.Builder b = new LatLngBounds.Builder();
            b.include(pos);
            b.include(pos1);
    
            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(b.build(),  20);
            mMap.animateCamera(cu, 10, new GoogleMap.CancelableCallback() {
                @Override
                public void onFinish() {
                    Log.e(TAG, "Start animate onFinish");
                    CameraPosition cp = new CameraPosition.Builder()
                            .zoom(mMap.getCameraPosition().zoom)
                            .target(pos)
                            .tilt(45.0f)
                            .bearing(35.0f)
                            .build();
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
    //                mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
                }
    
                @Override
                public void onCancel() {
                    Log.e(TAG, "Start animate onCancel");
                }
            });
    

    The result from my device:

    enter image description here

    Using the CancelableCallBack you can make the changes to the camera position as before, but if you do not change the zoom factor, the camera keeps the old zoom factor, you just set the bearing and tilt as you like.