
The following code sample demonstrates how to use
CameraRestriction
to restrict both the geographical boundaries of the camera and the values of
the camera's altitude, heading, and tilt. To use this code sample, follow
the instructions in
Setup and
Add a 3D map
to your app to set up your Android Studio project with a basic 3D map.
Then, add the following code to the
MainActivity.kt
file:
// Add imports and define constants import com.google.android.gms.maps3d.model.AltitudeMode import com.google.android.gms.maps3d.model.LatLngAltitude import com.google.android.gms.maps3d.model.cameraRestriction import com.google.android.gms.maps3d.model.latLngAltitude import com.google.android.gms.maps3d.model.latLngBounds import com.google.android.gms.maps3d.model.polygonOptions const val EMPIRE_STATE_BUILDING_LATITUDE = 40.748233 const val EMPIRE_STATE_BUILDING_LONGITUDE = -73.985663 private const val NYC_SOUTH_WEST_LAT = 40.68563088976172 private const val NYC_SOUTH_WEST_LNG = -74.05030430240065 private const val NYC_NORTH_EAST_LAT = 40.85649214337128 private const val NYC_NORTH_EAST_LNG = -73.80240973771173 private const val MAX_ALTITUDE_NYC_METERS = 10000.0 private const val MIN_ALTITUDE_NYC_METERS = 500.0 private val nycBounds = latLngBounds { northEastLat = NYC_NORTH_EAST_LAT northEastLng = NYC_NORTH_EAST_LNG southWestLat = NYC_SOUTH_WEST_LAT southWestLng = NYC_SOUTH_WEST_LNG } // Define the restrictions val nycCameraRestriction = cameraRestriction { minAltitude = MIN_ALTITUDE_NYC_METERS maxAltitude = MAX_ALTITUDE_NYC_METERS minHeading = 0.0 maxHeading = 360.0 minTilt = 0.0 maxTilt = 90.0 bounds = nycBounds }