animateCamera works and moveCamera doesn't for GoogleMap - Android

后端 未结 4 1474
感情败类
感情败类 2021-01-14 16:36

I need to move Camera to cover all Markers on it. So, I build LatLngBounds and then try to call mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLng

4条回答
  •  攒了一身酷
    2021-01-14 16:52

    As per documentation , this API can't be used before the map has undergone layout. It says

    Note: Only use the simpler method newLatLngBounds(boundary, padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary, width, height, padding) at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.

    But you can make use of newLatLngBounds() method in OnCameraChangeListener. Everything will work perfectly and you don't need to calculate screen size. As far as I know, this event occurs after map size calculation.

        mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    
        @Override
        public void onCameraChange(CameraPosition arg0) {
            // Move camera.
            mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 15));
            // Remove listener to prevent position reset on camera move.
            mMap.setOnCameraChangeListener(null);
        }
    });
    

提交回复
热议问题