Search code examples
javaandroidkotlingoogle-maps-android-api-2google-maps-android-api-3

Android Google Maps moveCamera causes "IllegalStateException: Illegal height"


When I call moveCamera on a small screen, it throws the following exception:

java.lang.IllegalStateException: Illegal height.

This is my code:

googleMap.moveCamera(
    CameraUpdateFactory.newLatLngBounds(
        bounds,
        // Padding
        100,
    ),
)

I tried removing the padding, but it makes no difference.

I guess this problem has to do with the limited height available to render the map as this only occurs on small screens, but there is still quite some space left for the map to render so I wouldn't expect a crash.

How do I render the map and move the camera to the given bounds?


Dependency versions:

  • com.google.android.libraries.maps:maps:3.1.0-beta
  • com.google.maps.android:maps-v3-ktx:2.2.0
  • androidx.compose.ui:ui:1.0.0-rc02

Solution

  • Even though the crash happened on moveCamera, the problem seemed to be caused by setPadding called above it:

    googleMap.setPadding(
        0,
        topPadding,
        0,
        0,
    )
    

    The fix was making sure that the padding would not exceed half of the map size:

    googleMap.setPadding(
        0,
        min(topPadding, map.height / 2 - 2),
        0,
        0,
    )