I am animating the camera to a LatLngBounds
with a padding set based on the percentage of the device width
so that it works on small devices.
This works even on small devices with 4inch displays, however it fails in multi-window mode in Android 7.0 and devices that support multi-window mode before that, eg. Galaxy S7.
I get the following exception on devices in multi-window mode:
Fatal Exception: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int, int, int): View size is too small after padding is applied.
Here is the suspect code:
private void animateCamera() {
// ...
// Create bounds from positions
LatLngBounds bounds = latLngBounds(positions);
// Setup camera movement
final int width = getResources().getDisplayMetrics().widthPixels;
final int height = getResources().getDisplayMetrics().heightPixels;
final int padding = (int) (width * 0.40); // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
mMap.animateCamera(cu);
}
How can I properly set the padding in newLatLngBounds
to work on all device widths and in multi-window mode?
The solution is to choose the minimum metric between width and height, since in Multi-window mode the height can be smaller than the width:
private void animateCamera() {
// ...
// Create bounds from positions
LatLngBounds bounds = latLngBounds(positions);
// Setup camera movement
final int width = getResources().getDisplayMetrics().widthPixels;
final int height = getResources().getDisplayMetrics().heightPixels;
final int minMetric = Math.min(width, height);
final int padding = (int) (minMetric * 0.40); // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
mMap.animateCamera(cu);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With